Lifting elements into components #697
-
I wonder what's the best practice for lifting elements into components. I have a helper function that works like this, but feels like this is (1) either an anti-pattern, or (2) there's a native way for this already. /// Lifts an FTXUI element into a component.
template <class T>
auto component(T x) -> ftxui::Component {
return ftxui::Renderer([x = std::move(x)]() {
return x;
});
} Any guidance would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello, My preference is making a class deriving from ComponentBase. This way, we don't make a deeply nested component. Only one: Component MakeOne() {
class Impl : public ComponentBase {
int counter_up = 0;
int counter_down = 0;
Element Render() final {
return vbox({
text("counter_up = " + std::to_string(counter_up)),
text("counter_down = " + std::to_string(counter_down)),
});
}
bool OnEvent(Event event) final {
if (event == Event::ArrowUp) {
counter_up++;
return true;
}
if (event == Event::ArrowDown) {
counter_down++;
return true;
}
return true;
}
};
return Make<Impl>();
}
|
Beta Was this translation helpful? Give feedback.
Hello,
You are free to use what works for you.
My preference is making a class deriving from ComponentBase. This way, we don't make a deeply nested component. Only one: