Created
March 26, 2016 07:36
-
-
Save pitkane/780bde3db52e06df2d7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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