Last active
August 29, 2015 14:27
-
-
Save tomchentw/8a202eef72ee640e1f6c to your computer and use it in GitHub Desktop.
Redux-Universal - Code Sections - 3
This file contains hidden or 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
| // http://git.io/v3IqX | |
| // define common function for fetching required data for this component. | |
| function fetchData (dispatch) { | |
| return dispatch(ForumActions.getForumList()); | |
| } | |
| // store is from the factory function. We extract our interested state for this component | |
| @createEnterTransitionHook(store => (/*state, transition */) => { | |
| const { AppReducer, ForumReducer } = store.getState(); | |
| // will be true only on server-side rendering. It should be set to false when dehydrate to client side. | |
| if (AppReducer.fetchForServerRendering) { | |
| return fetchData(store.dispatch); | |
| } | |
| // in client side, this component could be loaded multiple times. | |
| // This is often your business logic goes to check existing data in your store. | |
| if (ForumReducer.get("forumList").isEmpty()) { | |
| // Return a fetching promise if it’s necessary | |
| return fetchData(store.dispatch); | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment