Red Saga Preload Data When Changing Route

I want to load a dataset from api using the reduction itself, but I cannot find an example of how to do this when you go to a new route (e.g. / pillars) before displaying the route.

How can I do it?

+5
source share
1 answer

You can use onEnter prop on Route :

 <Route path='posts' onEnter={() => store.dispatch({ type: 'FETCH_POSTS' })} /> 

In this case, you cannot use the repository in the context of the tree that is inserted into the Provider . You will have to import it and use it directly.

Another option, instead of passing component prop, pass a getComponent :

 <Route path='posts' getComponent={(nextState, cb) => { store.dispatch({ type: 'FETCH_POSTS' }); cb(null, PostList); }} /> 

I can’t say what are the pros and cons for any of these approaches, although I have never used them. I just tried to find a solution that could work for your use case.

0
source

All Articles