Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple states and reducers #21

Closed
wcisco17 opened this issue Jun 15, 2019 · 6 comments
Closed

Multiple states and reducers #21

wcisco17 opened this issue Jun 15, 2019 · 6 comments

Comments

@wcisco17
Copy link

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?

@dai-shi
Copy link
Owner

dai-shi commented Jun 15, 2019

It looks OK at first glance...
Could you create reproduction in codesandbox?

@wcisco17
Copy link
Author

wcisco17 commented Jun 15, 2019

Here I'm running into this error trying to combine multiple state:
Is there any way to do this?

×
Error
Name not Found: 'counter'. It must be provided in initialState as a property key.

https://codesandbox.io/embed/spring-framework-rinu8?fontsize=14

@wcisco17
Copy link
Author

wcisco17 commented Jun 15, 2019

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

@dai-shi
Copy link
Owner

dai-shi commented 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.

@wcisco17
Copy link
Author

Thank you so much!

dai-shi added a commit that referenced this issue Jun 15, 2019
@dai-shi
Copy link
Owner

dai-shi commented Jun 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants