我无法从文档中弄清楚如何使用 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' })
.
但是当我fetchJokeSaga
直接使用as 时runSaga({ dispatch, getState: store.getState }, fetchJokeSaga);
,它会立即进行 api 调用。
我如何动态启动watcherSaga
以便我可以在以后调度“FETCH_JOKES”?