Allow getState in reducer
See original GitHub issueHere is a demo scene.
const repoFile = (state = {}, {type, payload}) => {
switch (type) {
case uiAction.route.type: {
if (REPO.FILE.REF.path === payload.path) {
const topState = getStore().getState();
const basic = getBasics(topState);
if(basic.defaultBranch===payload.params.ref){
return state;
}
return payload.params;
}
return state;
}
default:
return state;
}
};
I don’t get the reason to forbid getState() in reducers. I have read the source code.
function getState(): S {
if (isDispatching) {
throw new Error(
'You may not call store.getState() while the reducer is executing. ' +
'The reducer has already received the state as an argument. ' +
'Pass it down from the top reducer instead of reading it from the store.'
)
}
return currentState as S
}
If I can’t getState . I must to write a lot action to do this.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Redux: Calling store.getState() in a reducer function, is that an ...
Yes, this is absolutely an anti-pattern. Reducer functions should be "pure", and only based on their direct inputs (the current state and the...
Read more >Store - Redux
The store's reducing function will be called with the current getState() result and the given action synchronously. Its return value will be ...
Read more >Redux: Store Methods: getState(), dispatch(), and subscribe()
It lets you dispatch actions. When you create it, you need to specify the reducer that tells how state is updated with actions....
Read more >Actions and reducers: updating state - Human Redux
Let's look a bit deeper. ... getState() just returns the entire application state. ... For the time being let's write a reducer (or,...
Read more >What is Redux store? An explanation for beginners - Lightrains
Holds the current application state inside const store=createStore(reducer) · Allows access to the current state via store.getState() console.log ...
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
No, calling
getState
in a reducer is not allowed.Please see the Redux FAQ entry on how to “share state between slices” for suggestions.
It won’t, don’t worry 😉 Personal experience