question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow getState in reducer

See original GitHub issue

Here 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Sep 19, 2019

No, calling getState in a reducer is not allowed.

Please see the Redux FAQ entry on how to “share state between slices” for suggestions.

0reactions
ematipicocommented, Sep 19, 2019

I am afraid a middleware make app slow.

It won’t, don’t worry 😉 Personal experience

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found