Search code examples
reactjs-fluxflux

Should view call the store directly?


From the Flux's TodoMVC example, I saw the TodoApp component is asking the store to get the states.

Should the view create the action and let the dispatcher to call the store instead?


Solution

  • The views that are listening for the stores' "change" event are called controller-views, because they have this one controller-like aspect: whenever the stores change, they get data from the stores and pass it to their children through props.

    The controller-views are the only views that should be calling the stores' getters. The getters should be the only public API that the stores expose. Stores have no setters.

    It's very tempting to call the stores' getters within the render() method of some component deep in the tree, but this is an anti-pattern. It violates the unidirectional data flow, making it more difficult to understand the flow of data through the application, and it and makes your rendering more expensive.

    In the TodoMVC Flux example, the TodoApp component is the only controller-view.