I am not able to figure out from the documentation how to start a redux saga watcher using runSaga. Suppose I have the following in 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);
}
and the following in 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;
};
I'm not able to trigger the api call using dispatch({ type: 'FETCH_JOKE' })
.
But when I use fetchJokeSaga
directly as runSaga({ dispatch, getState: store.getState }, fetchJokeSaga);
, it makes the api call immediately.
How do I dynamically start watcherSaga
so that I can dispatch 'FETCH_JOKES' later?