I'm studying Flux and I think I understood the workflow:
View -> Action -> Dispatcher -> Store -> View
However, I didn't quite understand where am I supposed to populate the initial state of my Stores.
For instance, let's say that I'm editing a Contact. So I'd assume I'd have a ContactsStore
. This is what I imagine would happen when I access the URL /contacts/edit/23
:
ContactsStore
gets populated with the contact I'm editing, in this case, contact 23. The data would come from the server.EditContact
view would receive a notification from the ContactsStore
, so it would render itself in the initial state.SaveContact
action and the flow would go on.Step (1) is not clear to me. Where is the ContactsStore
expected to be populated with the initial state? Where do I call the server? Is it on the Store?
Thanks.
Node's event emitter is used to update the store and broadcast the update to view. The view never directly updates the application state. It is updated because of the changes to the store. This is only part of Flux that can update the data.
In Flux application, data flows in a single direction(unidirectional). This data flow is central to the flux pattern. The dispatcher, stores, and views are independent nodes with inputs and outputs. The actions are simple objects that contain new data and type property.
Store − Store is the place where the application state and logic are held. Every store is maintaining a particular state and it will update when needed. View − The view will receive data from the store and re-render the app.
Flux's slogan is “unidirectional data flow”.
You need to have access to the action and the store in your EditContact
component. In the componentDidMount
handler you could fire an action which does the api request. On success it passes the contact to the dispatcher
/store
. As soon as the store receives the contact
it fires an event on which the EditContact
component has subscribed. In the corresponding handler the component sets the new state with the new contact. I'm sure there are other ways to do this, but that's how I would do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With