Eoin Moloney
Eoin Moloney

Reputation: 61

React passing state between instances of the same Component

I'm fairly new to React, and I was trying to create an app that functioned thusly:

Thanks.

Upvotes: 0

Views: 2672

Answers (2)

Ali Tajeldin
Ali Tajeldin

Reputation: 61

One option to consider is to use React Redux to store the state of your application. You would then use mapStateToProps (See Redux API for details) to map the state into props for your stats component.

Upvotes: 1

jered
jered

Reputation: 11591

State should live above the level of all components that need access to that state.

Remember that one of the principles of React is "one-way" data flow down the component hierarchy. Essentially, data/state should live at a high level, getting passed down to child components and consumed as needed.

In your case, you have some "stats" data that needs to be displayed across multiple Pages. So, "stats" needs to be owned by a component above all of your Page components - perhaps at the root component of the app itself. Pages themselves would just take the data in and render it, potentially with some callbacks appropriate for editing the data.

Read a bit more about Facebook's philosophy for React in "Thinking in React" in the official docs: https://facebook.github.io/react/docs/thinking-in-react.html#step-4-identify-where-your-state-should-live

Upvotes: 2

Related Questions