【发布时间】:2019-09-09 17:26:50
【问题描述】:
我无法从文档中找出如何使用 runSaga 启动 redux saga watcher。假设我在saga.js 中有以下内容:
export function* fetchJokeSaga(action) {
try {
const response = yield call(axios.get, "...");
yield put({ type: "UPDATE_JOKE", payload: response });
} catch (e) {}
}
export default function* watcherSaga(action) {
yield takeEvery("FETCH_JOKE", fetchJokeSaga);
}
以及Component.js中的以下内容:
const Component = () => {
const store = useStore();
const dispatch = useDispatch();
const { joke } = useSelector(state => state);
React.useEffect(() => {
runSaga({ dispatch, getState: store.getState }, watcherSaga);
}, []);
return joke;
};
我无法使用 dispatch({ type: 'FETCH_JOKE' }) 触发 api 调用。
但是当我直接将fetchJokeSaga 用作runSaga({ dispatch, getState: store.getState }, fetchJokeSaga); 时,它会立即调用api。
如何动态启动watcherSaga,以便稍后调度“FETCH_JOKES”?
【问题讨论】:
标签: reactjs redux generator redux-saga