-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
firing actions in response to route transitions in react-router #227
Comments
@deowk You could compare |
@johanneslumpe: In my case campaigngroups is a rather large collection of objects, it seem kind of inefficient use a deep object comparison as a condition in this way? |
@deowk if you use immutable data, you can just compare references |
@deowk There are two parts to this problem, I'd say. The first is that The trick is to create an action type that fires whenever the router location changes. This is easy in the upcoming 1.0 version of React Router: // routeLocationDidUpdate() is an action creator
// Only call it from here, nowhere else
BrowserHistory.listen(location => dispatch(routeLocationDidUpdate(location))); Now your store state will always be in sync with the router state. That fixes the need to manually react to query param changes and <Connector select={state => ({ filter: getFilters(store.router.params) })} /> The second part of the problem is you need a way to react to Redux state changes outside of the view layer, say to fire an action in response to a route change. You can continue to use componentWillReceiveProps for simple cases like the one you describe, if you wish. For anything more complicated, though, I recommending using RxJS if you're open to it. This is exactly what observables are designed for — reactive data flow. To do this in Redux, first create an observable sequence of store states. You can do this using redux-rx's import { observableFromStore } from 'redux-rx';
const state$ = observableFromStore(store); Then it's just a matter of using observable operators to subscribe to specific state changes. Here's an example of re-directing from a login page after a successful login: const didLogin$ = state$
.distinctUntilChanged(state => !state.loggedIn && state.router.path === '/login')
.filter(state => state.loggedIn && state.router.path === '/login');
didLogin$.subscribe({
router.transitionTo('/success');
}); This implementation is much simpler that the same functionality using imperative patterns like Hope that helps! |
We need this in new docs. So well written. |
@acdlite: Thank you very much, your advise really helped me out a lot, I am struggling with the following though --> changing the state in the store by triggering an action creator in response to a url change. I want to trigger the action creator in the willTransitionTo static method since this seems like the only option in react-router v0.13.3
|
@deowk My guess is that you call
|
@deowk I currently dispatch my url changes in react router 0.13.3 like this:
|
@acdlite really well explained! 👍 |
Just as a note, https://github.com/rackt/react-router/blob/master/modules/BrowserHistory.js#L57 EDIT: Which might look like this:
|
And what about the initial state for that reducer, @pburtchaell? |
I have the following setup: // client.js
class App extends Component {
constructor(props) {
super(props);
this.history = new HashHistory();
}
render() {
return (
<Provider store={store}>
{renderRoutes.bind(null, this.history)}
</Provider>
);
}
}
// routes.js
export default function renderRoutes(history) {
// When the route changes, dispatch that information to the store.
history.addChangeListener(() => store.dispatch(routeLocationDidUpdate(location)));
return (
<Router history={history}>
<Route component={myAppView}>
<Route path="/" component={myHomeView} />
</Route>
</Router>
);
}; This fires the |
@pburtchaell can you share your |
@gyzerok Sure. It is an action creator. // actions/location.js
export function routeLocationDidUpdate(location) {
return {
type: 'LOCATION_UPDATE',
payload: {
...location,
},
};
} |
@pburtchaell so in your example I dont quiet understand where do you fetch data nessesary for a particular route |
Here's a fuller version of my example above: |
I assume @pburtchaell would use an async version of that code to get data (from a REST call etc). What i'm unsure about is what then? I assume it then goes to a store, but would that store be a locationStore that i then
to in, let's say in
which would already be 'connected' (subscribed) to a commentStore? Or just add register these location actions in commentsStore? An url like
is always going to be 'coupled' to the comments component, so how do i reuse it? Sorry if this is dumb, very confused here. Which there was a solid example I could find. |
I'm also grappling with this, figuring out how to call my static Since a reference to The latter is more concise but breaks the fact that |
My solution is this, although quite tailored to my set up (static |
We definitely need to add in the docs! Possible in "Using with react-router" section. |
We'll have an official way of doing that after 1.0 release. |
Great to hear @gaearon. |
I've got an observable stream set up to keep my store in sync with any route changes, and I've got a stream setup to watch my store too. I'm interested in transitioning to a new route when when a value changes in my store, specifically when it changes from The two streams are set up and working, and the store is being updated, but I'm new to Rx though, and having trouble with the observable query ... any pointers? Am I on the right track? Pretty sure its got something to do with EDIT: Ack! Sorry, answered my own question. Pseudo code below. Let me know if it looks horrid? const didChangeProject$ = state$
.distinctUntilChanged(state => state.project._id)
.filter(state => typeof state.project._id !== 'undefined'); |
Closing in favor of #177. When we get to documenting routing, we'll need to cover all scenarios: redirect on state change, redirect on request callback, etc. |
I know this is closed.. but with React 0.14 and the various 1.0 dependencies in beta right now, is there an update to this and if not, can a tutorial around the new abilities in React 0.14, react-router, redux, etc be written? There seem to be a lot of us that are trying to stay up to date with the upcoming changes while learning/understanding all this at the same time. I know things are in a state of flux (pun.. uh.. not intended), but it seems I am seeing pieces from different examples that dont play nice with one another and after several days I still cant get my updated app to work with routing. Most likely my own fault as I am still struggling a bit with understanding how all this works, just hoping an updated tutorial with the latest stuff is out soon. |
Until there is a tutorial feel free to look at |
Recommended reading: http://stackoverflow.com/questions/35665724/with-react-redux-router-how-should-i-access-the-state-of-the-route?rq=1 reduxjs/redux#227 (comment) jlongster/react-redux-universal-hot-example@e13b935?diff=split acdlite/redux-router#172 https://github.com/acdlite/redux-rx#createconnectorselectstate-render https://shellmonger.com/2016/03/08/react-redux-and-routing/ https://blog.risingstack.com/react-js-best-practices-for-2016/ https://github.com/reactjs/redux/tree/master/examples/real-world http://stackoverflow.com/questions/35196873/how-to-use-react-router-redux-routeactions/37494808 http://stackoverflow.com/questions/36722584/how-to-sync-redux-state-and-url-hash-tag-params/36749963#comment61535948_36749963 https://github.com/Automattic/wp-calypso/blob/master/docs/our-approach-to-data.md#third-era-redux-global-state-tree-december-2015---present
Recommended reading: http://stackoverflow.com/questions/35665724/with-react-redux-router-how-should-i-access-the-state-of-the-route?rq=1 reduxjs/redux#227 (comment) jlongster/react-redux-universal-hot-example@e13b935?diff=split acdlite/redux-router#172 https://github.com/acdlite/redux-rx#createconnectorselectstate-render https://shellmonger.com/2016/03/08/react-redux-and-routing/ https://blog.risingstack.com/react-js-best-practices-for-2016/ https://github.com/reactjs/redux/tree/master/examples/real-world http://stackoverflow.com/questions/35196873/how-to-use-react-router-redux-routeactions/37494808 http://stackoverflow.com/questions/36722584/how-to-sync-redux-state-and-url-hash-tag-params/36749963#comment61535948_36749963 https://github.com/Automattic/wp-calypso/blob/master/docs/our-approach-to-data.md#third-era-redux-global-state-tree-december-2015---present
Recommended reading: http://stackoverflow.com/questions/35665724/with-react-redux-router-how-should-i-access-the-state-of-the-route?rq=1 reduxjs/redux#227 (comment) jlongster/react-redux-universal-hot-example@e13b935?diff=split acdlite/redux-router#172 https://github.com/acdlite/redux-rx#createconnectorselectstate-render https://shellmonger.com/2016/03/08/react-redux-and-routing/ https://blog.risingstack.com/react-js-best-practices-for-2016/ https://github.com/reactjs/redux/tree/master/examples/real-world http://stackoverflow.com/questions/35196873/how-to-use-react-router-redux-routeactions/37494808 http://stackoverflow.com/questions/36722584/how-to-sync-redux-state-and-url-hash-tag-params/36749963#comment61535948_36749963 https://github.com/Automattic/wp-calypso/blob/master/docs/our-approach-to-data.md#third-era-redux-global-state-tree-december-2015---present
Recommended reading: http://stackoverflow.com/questions/35665724/with-react-redux-router-how-should-i-access-the-state-of-the-route?rq=1 reduxjs/redux#227 (comment) jlongster/react-redux-universal-hot-example@e13b935?diff=split acdlite/redux-router#172 https://github.com/acdlite/redux-rx#createconnectorselectstate-render https://shellmonger.com/2016/03/08/react-redux-and-routing/ https://blog.risingstack.com/react-js-best-practices-for-2016/ https://github.com/reactjs/redux/tree/master/examples/real-world http://stackoverflow.com/questions/35196873/how-to-use-react-router-redux-routeactions/37494808 http://stackoverflow.com/questions/36722584/how-to-sync-redux-state-and-url-hash-tag-params/36749963#comment61535948_36749963 https://github.com/Automattic/wp-calypso/blob/master/docs/our-approach-to-data.md#third-era-redux-global-state-tree-december-2015---present
Hi Guys,
I am using react-router and redux in my latest app and I'm facing a couple of issues relating to state changes required based on the current url params and queries.
Basically I have a component that needs to update it's state every time the url changes. State is being passed in through props by redux with the decorator like so
At the moment I am using the
componentWillReceiveProps
lifecycle method to respond to the url changes coming from react-router since react-router will pass new props to the handler when the url changes inthis.props.params
andthis.props.query
- the main issue with this approach is that I am firing an action in this method to update the state - which then goes and passes new props the component which will trigger the same lifecycle method again - so basically creating an endless loop, currently I am setting a state variable to stop this from happening.Is there a standard approach to trigger actions base on route transitions OR can I have the state of the store directly connected to the state of the component instead of passing it in through props? I have tried to use
willTransitionTo
static method but I don't have access to the this.props.dispatch there.The text was updated successfully, but these errors were encountered: