How to use Events in a reusable way? #410
Replies: 1 comment
-
Yes there's a couple of possibilities here. Firstly, I should note that in this case the counter can't really be a self-contained component, because the data which it binds to is external to the component and has to be mutated by the model. I might change the language in the guide to reflect this because I know that in other frameworks a component is completely self-contained and is also responsible for mutating the state. Anyway, on to the solution. There's two approaches which work equally well here.
Counter::new(cx, AppData::count)
.on_increment(|cx| cx.emit(AppEvent::Increment)); In both cases it's still up to the model to respond to events and actually mutate the data. Which you choose depends on how you want to modularise your code. In the first the app data needs to know about the counter events. I personally prefer the latter because it means I can have events which relate specifically to the model (e.g. I'll update the guide with these suggestions. |
Beta Was this translation helpful? Give feedback.
-
I'm following the getting started guide, and I'm on the "Making the Counter Reusable" page at: https://book.vizia.dev/quickstart/components.html
It shows how to build the ui elements into their own view and how to pass in a lens. However, it seems like the events that the buttons emit must be hardcoded at the application level so that the app can respond to them. Is it possible to define the event enum inside the custom view and still respond to this event type from an app, potentially in another crate.
Beta Was this translation helpful? Give feedback.
All reactions