Skip to content

Instantly share code, notes, and snippets.

@tomchentw
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save tomchentw/8a202eef72ee640e1f6c to your computer and use it in GitHub Desktop.

Select an option

Save tomchentw/8a202eef72ee640e1f6c to your computer and use it in GitHub Desktop.
Redux-Universal - Code Sections - 3
// 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