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.

Multiple states and reducers

See original GitHub issue

I have 2 files as of right now and both of them have different initialState and reducers, but I’ve been having some trouble combining them into one

state.ts

const userState: IUState = {
      user: null,
}

const counterState: ICState = {
     counter: number,
}

const state = {
    userState,
    counterState
}

const reducer = combineReducers({
    counter: counterReducer as Reducer<State, ICAction>,
    user: userReducer as Reducer<State, IUAction>,
});

export const { GlobalStateProvider, dispatch, useGlobalState } = createStore(
    reducer,
    state as IState<typeof initialState> | any
);

It’s showing up as undefined when I try to run the counter is there anyway to combine them together?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Jun 15, 2019

The error message means the (combined) state doesn’t have counter property.

https://codesandbox.io/s/trusting-ganguly-briq8

Here’s the fixed version. Apart from typing, two major notes:

  • when using combineReducers, it needs to specify default state in reducer.
  • useGlobalState has to be used inside of <GlobalStateProvider>

For the latter, I will think about if I can show a better error message. Thanks for your feedback.

1reaction
wcisco17commented, Jun 15, 2019

By the way, love the library! You did a really good job on making the whole redux like concept super simple.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I have multiple states from one reducer in Redux?
If they are not separate states but instead interconnected properties, then use a single reducer, and give the state 2 properties, pokemon and ......
Read more >
Redux Fundamentals, Part 3: State, Actions, and Reducers
Reducers are functions that take the current state and an action as arguments, and return a new state result. In other words, (state,...
Read more >
Redux From Scratch (Chapter 8 | Dividing the State ... - Medium
So far, we have managed everything with one state and one reducer. In this chapter, we learn how to divide our state with...
Read more >
Multiple States vs Reducers and the convention for type and ...
Firstly, the only way to understand, why do you need a reducer is to create a use case and solve it with multiple...
Read more >
Redux Toolkit Multiple State Changes in One Reducer Code ...
OP is return ing the result of their change. If you are returning a new state from a reducer, it must be an...
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