我的文件夹结构:
|--App
|--Components
|--PageA.js
|--PageB.js
|--PageC.js
|--common-effects
|--useFetching.js
我正在重构我的代码以使用 react hooks从 API 获取数据。我想从useFetching.js中的useEffect调度一个被saga中间件拦截的动作。仅当组件(PageA、PageB、PageC)安装时才应分派该操作。
我正在使用redux、react-redux和redux-saga。
页面A.js :
function(props) {
useFetching(actionParams)
//....//
}
PageB和PageC组件的类似代码。
我已经抽象了可重用代码以在useFetching
Custom hook 中获取数据。
使用Fetching.js
const useFetching = actionArgs => {
useEffect( () => {
store.dispatch(action(actionArgs)); // does not work
})
}
我不知道如何dispatch
在useFetching 中访问 redux 。我试了一下,useReducer
效果不错,但传奇错过了动作。