How to dispatch Redux action from stateless component when route is loaded?

前端 未结 5 1897
离开以前
离开以前 2020-12-15 17:03

Goal: when loading a react-router route, dispatch a Redux action requesting asynchronic Saga worker to fetch data for the underlying stateless component of

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 17:25

    I think I found the cleanest solution without having to use stateful components:

    const onEnterAction = (store, dispatchAction) => {
        return (nextState, replace) => {
            store.dispatch(dispatchAction());
        };
    };
    
    const myDataFetchAction = () => ({ type: DATA_GET_REQUEST });
    
    export const Routes = (store) => (
        
    );
    

    The solution passes the store to a higher order function that is passed to the onEnter lifecycycle method. Found the solution from https://github.com/reactjs/react-router-redux/issues/319

提交回复
热议问题