Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to organize redux with react-router? #915

Closed
ramin32 opened this issue Oct 19, 2015 · 3 comments
Closed

How to organize redux with react-router? #915

ramin32 opened this issue Oct 19, 2015 · 3 comments
Labels

Comments

@ramin32
Copy link

ramin32 commented Oct 19, 2015

Redux advocates the use of a single store with a single state. However, with react-router you get multiple pages each having their own top-level root component.

How should one go about wiring up redux with a react-router app that has multiple pages? React-router 1.0 no longer lets you pass props to the routes so making the router the top level component that contains the state for all the pages is no longer possible nor is it feasible.

A simple example would be appreciated.

@gaearon
Copy link
Contributor

gaearon commented Oct 19, 2015

examples/real-world uses React Router through Redux Router.
This is the approach we recommend.
See also #637 for discussion.

React-router 1.0 no longer lets you pass props to the routes so making the router the top level component that contains the state for all the pages is no longer possible nor is it feasible.

Having just one connected root component is overkill anyway. Connecting route handlers is perfectly reasonable from performance and other standpoints. You still have one store. Please see #419 for discussion.

@ramin32
Copy link
Author

ramin32 commented Oct 19, 2015

@gaearon, I am new with redux, but I am assuming by "connected" you mean doing something like store.subscribe(...) inside of the component.

When I subscribe inside of the top level component that renders that particular page, do I simply set the state of that component with a subtree of the store state?

For example:

appState = {
   user: {...},
   posts: [],
   messages: []
}

in PostsComponent I would do:

var PostComponent = React.createClass({
   componentDidMount: function () {
       store.subscribe(() =>
          this.setState({posts: store.getState().posts})
      );
   }
   ...
});

@gaearon
Copy link
Contributor

gaearon commented Oct 19, 2015

You could do this, but React Redux is more efficient so you should use it instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants