Actions Are Called Twice
See original GitHub issueVery cool library, I like the approach. Thanks for building and OSing it!
I’ve swapped in redux-pack for redux-thunk and rewritten an action called getBroadcasts
from this redux-thunk action:
export function getBroadcasts() {
return async (dispatch) => {
dispatch({ type: 'GET_BROADCASTS_INITIATED' });
try {
const { data: { results } } = await api.get('/broadcasts');
dispatch({ type: 'GET_BROADCASTS_SUCCEEDED', payload: results });
} catch (error) {
dispatch({ type: 'GET_BROADCASTS_FAILED', payload: error });
}
};
}
to this redux-pack action:
export function getBroadcasts() {
return {
type: 'GET_BROADCASTS',
promise: api.get('/broadcasts')
}
}
What I observe in the console when the relevant page loads is that while the redux-thunk action was called only once:
the redux-pack action is called twice, and returns once with an undefined payload, and once with a successful payload:
I assume this is expected behavior, I just wanted to confirm.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
useReducer dispatch being called twice - Stack Overflow
It's almost working fine, but it is executing the actions twice. The toggle function for example is putting the attribute done to true,...
Read more >Why are my Redux actions being dispatched twice? : r/reactjs
It is because the uuid object gets created again on each render so the reference to that object is never the same. That's...
Read more >useReducer dispatch calls reduce twice · Issue #16295 - GitHub
There is no "problem". React intentionally calls your reducer twice to make any unexpected side effects more apparent. Since your reducer is ...
Read more >React 18 - Avoiding Use Effect Getting Called Twice
For React Hooks in React 18, this means a useEffect() with zero dependencies will be executed twice. Here is a custom hook that...
Read more >Action is called twice in single request in grails
Sometime it happens that controller's action/method is called twice back to back.At first time it is called with the right parameter you have...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It looks like there are intentionally 2 dispatches, on start and then on one of success or failure.
The nice part of having the 3-phase action (old way) is that you can have other reducers react to those actions. I think you lose that here, but maybe I’m missing something?
Closing as this has been confirmed as expected behavior. Thanks for the discussion!