call injectReducer in `onEnter()` if fetch async data in react-router onEnter hook
I came with a problem that I should fetch async data with react-router's onEnter hook.I just follow the approach of Counter example, dispatch a thunk in onEnter() , and inject the relative reducer in getComponent() as below:
export default (store) => ({
path: 'panel/:panelId',
getComponent(nextState, cb) {
...
require.ensure([], (require) => {
const Panel = require('./containers/PanelContainer').default
const reducer = require('./modules/panel').default
/* Add the reducer to the store on key 'panel' */
injectReducer(store, { key: 'panel', reducer })
/* Return getComponent */
cb(null, Panel)
/* Webpack named bundle */
}, 'panel')
},
onEnter: (nextState) => {
store.dispatch(fetchPanelConfig(nextState.params.panelId))
},
})
Something strange happened here. Because the 'injectReducer()' inject reducer in async way, we can't ensure that the 'panel' reducer is injected properly before 'fetchPanelConfig' action finished. Behavior of program became weird. I just try to move the 'indectReducer' in 'onEnter' method, and everything works well.
onEnter: (nextState) => {
const reducer = require('./modules/panel').default
/* Add the reducer to the store on key 'panel' */
injectReducer(store, { key: 'panel', reducer })
store.dispatch(fetchPanelConfig(nextState.params.panelId))
},
I was still confused of how 'require.ensure' works here, any explanation is appreciated.
I guess you already solved the issue, but just for the record. When dealing with API calls you can have three actions, each of them dispatched on different cycles of the fetching process.
1- When the data is requested: Dispatch an action that sets the state indicating to the user that something is loading, usually a flag isFetching and render a loading icon. 2- When data is finally fetched: Dispatch another action that sets the isFetching flag to false and renders the component with the corresponding data. 3- In case you get an error: Then dispatch an action that sets the state so that the user is notified about the problem .
I'm just starting with Redux so I might be wrong, feel free to confirm this in their documentation.
Your solution of colocating the injection and dispatch is correct :).
@huangzhuolin with this solution fetchPanelConfig fires on every child route (eg :panelId). Is there a way to fire onEnter on the parent route only?
@foodgy I guess you just want to fetch some data at parant path eg, /panel. At this point you may need to define your parent Router explicitly and put the fetch logic there.
@huangzhuolin well, I do have separate route config for parent and child.
Parent route configuration example (list of projects):
export default store => ({
path: 'projects',
childRoutes: [createProjectRoute(store)], // Child route for project
onEnter: nextState => {
const projects = require('./modules/projects').default
injectReducer(store, {
key: 'projects',
reducer: projects
})
/* Fetch List of Projects */
store.dispatch(fetchProjects())
},
getIndexRoute(nextState, cb) {
/* IndexRoute for Project List page */
...
}
})
Child project route configuration looks like this:
export default store => ({
path: ':id',
getComponent(nextState, cb) {
/* Fetching Project Data here */
}
})
Whit this setup onEnter of /propjects fires even if user directly loads /projects/:id. Just curious if this is expected behaviour. BTW I've read that onEnter (as router config in general) is not the best place to fetch data.
@foodgy Yes. onEnter of /projects will be fired if you directly loads /projects/:id. Another way to fetch initial data of your app is to fetch data in your component's componentDidMount(). Refer this:
componentdidmount
@huangzhuolin thanks!