React + Flux - should store data be stored in a component state, or props?

Guy Nesher picture Guy Nesher · Aug 22, 2014 · Viewed 18.5k times · Source

If that the flux store is a singleton that maintains the state of the data why do the components use setState and not setProps when accessing the stores? Wouldn't it just mean that I started saving the application state in two (or more) places?

Both the Flux / React documentation and Examples seem to point to setState as the preferred solution, but I've had an interesting conversation with a few colleagues at work and wondered if anyone else came across this

Edit: You can see what I'm talking about in this url: https://github.com/facebook/flux/blob/master/examples/flux-chat/js/components/ThreadSection.react.js

Notice how ThreadSection is a child component, that is fetching data directly from a store and using it as a state.

If you follow the React "way" I would have expected the state to be managed by the store - not a child component.

The solution we thought of is to fetch all stores in the top level component (as props) and pass them down to the child components as needed. But that gets rather ugly rather quickly.

We do that because setProps does not work on child components

Answer

Rygu picture Rygu · Aug 24, 2014

Understand that you should have 2 kinds of components. Stateful components and view components.

Stateful components can have 3 kinds of states: initial state, user input state, and data store state.

Stateful components are like small entry points in the "widget" that you're assembling. There is no single application-wide entry point anymore for downstream dependency or data injection, because all of these widgets have their own isolated lifecycles. That's why they themselves need to access & listen to stores.

Besides behavorial properties, stateful components do not receive actual data via upstream properties.

Stateful components manage their own state and pass it to their children to render through downstream properties.

Stateful components do not normally render html DOM elements themselves directly. They're more like the controllers in MVC, and use other dumber components, the ones like views in MVC, to actually render DOM elements.

Dumber components are like views so they only contain logic to render DOM elements. Think of them as handlebars.js templates that only receive properties, and simply render those into DOM elements possibly with loops etc. They are stateless renderers.

Hope this answers your question.