How to test API request failures with Redux Saga?

Mark’s answer is correct. Middleware executes those instructions. But this makes your life easier: in the test, you can provide whatever you want as the argument to next(), and the generator function will receive it as a result of yield. This is exactly what saga middleware does (except that it actually fires up a request … Read more

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

I don’t know why you absolutly want a stateless component, while a stateful component with componentDidMount would do the job in a simple way. Dispatching actions in mapDispatchToProps is very dangerous and may lead to dispatching not only on mount but whenever ownProps or store props changes. Side effects are not expected to be done … Read more

How to “yield put” in redux-saga within a callback?

One possible solution, as you already mentioned, is to use channels. Here is an example that should work in your case: import { channel } from ‘redux-saga’ import { put, take } from ‘redux-saga/effects’ const downloadFileChannel = channel() export function* loadFile(id) { … const download = RNFS.downloadFile({ … // push `S_PROGRESS` action into channel on … Read more

React hooks: dispatch action from useEffect

Version using react-redux hooks: You can even cut out the connect function completely by using useDispatch from react-redux: export default function MyComponent() { useFetching(fetchSomething); return <div>Doing some fetching!</div> } with your custom hook import { useDispatch } from ‘react-redux’; const useFetching = (someFetchActionCreator) => { const dispatch = useDispatch(); useEffect(() => { dispatch(someFetchActionCreator()); }, []) … Read more

what is main difference between redux and redux toolkit, is saga required in redux toolkit? [closed]

Redux used to be great but if you have tried none of them, I would highly recommend using Redux-Toolkit. The only case where I may want you to stick to redux is when you’re using class-based components, where Redux Toolkit does have some boilerplate (like Redux) and you may miss out decent support. However with … Read more

How do I check for token expiration and logout user?

In my view middleware will be the best option. You can do something like this const checkTokenExpirationMiddleware = store => next => action => { const token = JSON.parse(localStorage.getItem(“user”)) && JSON.parse(localStorage.getItem(“user”))[“token”]; if (jwtDecode(token).exp < Date.now() / 1000) { next(action); localStorage.clear(); } next(action); }; You have to then wrap it in applyMiddleware

What is the idiomatic way of starting rootSaga?

How to create rootSaga? According to a core developer of redux-saga [1,2] the idiomatic way to create rootSaga is to use the all Effect Combinator. Also, please note that yielding arrays from sagas is deprecated. Example 1 You could use something like this (+all) import { fork, all } from ‘redux-saga/effects’; import firstSaga from ‘./firstSaga’; … Read more

tech