Skip to content

Instantly share code, notes, and snippets.

@pitkane
Created March 26, 2016 07:36
Show Gist options
  • Save pitkane/780bde3db52e06df2d7d to your computer and use it in GitHub Desktop.
Save pitkane/780bde3db52e06df2d7d to your computer and use it in GitHub Desktop.
// Most of the time you can do data loading in react-router's onEnter/onLeave hooks, which support async operations. If you're using Redux or some other flux implementation, you can dispatch a loading() action, perform your data fetching, and then dispatch a loaded() action once the data's ready.
function fetchData(nextState, replace, done) {
store.dispatch(loading());
doAsyncThing().then(() => {
store.dispatch(loaded());
done();
});
}
<Route
path="/whatev"
onEnter={ fetchData }
component={ Whatev }
/>
// The Whatev component can then use connect decorator from react-redux to pipe in the data as props.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment