Timothy Chu
Timothy Chu

Reputation: 153

Flux: How to make Stores for a Variable Number of React Components?

In the flux tutorial, it says that "Application state is maintained only in the stores."

Short version: I have a variable number of components in my app, each of which maintains state. How am I supposed to use stores here? It seems like I need one store per component.

Long version: I wanted to create a tree of text elements with a variable number of nodes (essentially an app that just lets me create an arbitrary tree and visualize it in HTML).

My current architecture has a Tree element in React, maintains the state:

{ children: [], // children are trees.
parent: someParent }

However, given that each subtree in my tree has state, it appears that I will need one store per subtree! What is the correct way to architect this code?

Upvotes: 0

Views: 61

Answers (2)

Piyush.kapoor
Piyush.kapoor

Reputation: 6803

Use Redux architecture

Keep your state global,

use container components and reducers to pass props to your components.

http://redux.js.org/docs/api/bindActionCreators.html

Upvotes: 0

santiagobasulto
santiagobasulto

Reputation: 11686

No, keep the state in just 1 component. All the other components should get their data in the form of props.

In addition, the component that keeps the state shouldn't be aware of the state, but you should be using a store to keep it. Generally, these types of components that know about the state, are named "Container Components". Those components that don't know about state and receive their data with props are called "Presentational Components" (or "dumb components"). Read more here: http://redux.js.org/docs/basics/UsageWithReact.html

Upvotes: 1

Related Questions