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

Initialize with null #6

Closed oertels closed 5 years ago

oertels commented 5 years ago

Hi, a question: I would like to initialize a value with null, something like

const initialState = {
  user: null
};
const [_, setUser] = useGlobalState<IUser | null>("user");

I get an error:

Type error: Type 'IUser' does not satisfy the constraint '"user"'

Is there any workaround?

Thanks!

dai-shi commented 5 years ago

In this case, you probably need to add a type in createGlobalState. (as the useGlobalState type is inferred from it.)

type State = {
  user: IUser | null;
};

const { GlobalStateProvider, useGlobalState } = createGlobalState<State>(initialState);
oertels commented 5 years ago

Thanks, got it working that way.