Multiple states and reducers
See original GitHub issueI 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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top 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 >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
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:
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.
By the way, love the library! You did a really good job on making the whole redux like concept super simple.