Skip to content

Lifting elements into components #697

Answered by ArthurSonzogni
mavam asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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;
      }

      

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mavam
Comment options

Answer selected by mavam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants