Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On React Flux, where I am supposed to populate the initial state of my Stores?

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:

  1. Somehow my ContactsStore gets populated with the contact I'm editing, in this case, contact 23. The data would come from the server.
  2. The EditContact view would receive a notification from the ContactsStore, so it would render itself in the initial state.
  3. As I save the contact, the view would trigger the 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.

like image 506
André Pena Avatar asked Jul 23 '15 13:07

André Pena


People also ask

Which building blocks of flux is used to grab the updated state from the store?

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.

Which of the following correctly describes the data flow of flux in React?

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.

Which is the place where the application state and logic are held?

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.

Which of the following correctly describes the data flow of flux?

Flux's slogan is “unidirectional data flow”.


1 Answers

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.

like image 180
Florian Gl Avatar answered Sep 29 '22 15:09

Florian Gl