dai-shi / react-hooks-global-state

[NOT MAINTAINED] Simple global state for React with Hooks API without Context API
https://www.npmjs.com/package/react-hooks-global-state
MIT License
1.1k stars 62 forks source link

Multiple states and reducers #21

Closed wcisco17 closed 5 years ago

wcisco17 commented 5 years ago

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 commented 5 years ago

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

wcisco17 commented 5 years ago

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 commented 5 years ago

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

dai-shi commented 5 years ago

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:

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

wcisco17 commented 5 years ago

Thank you so much!

dai-shi commented 5 years ago

Published: https://www.npmjs.com/package/react-hooks-global-state/v/0.14.0