Skip to content

Instantly share code, notes, and snippets.

@Munawwar
Last active October 25, 2020 06:22
Show Gist options
  • Save Munawwar/c090f66ad7b084c718739b9cc6d07cfc to your computer and use it in GitHub Desktop.
Save Munawwar/c090f66ad7b084c718739b9cc6d07cfc to your computer and use it in GitHub Desktop.
Zustand state manager usage
import create, { State, StateSelector } from 'zustand';
// initialization
const initialStates = { count: 0 }; // no need to add your actions here like how zustand README shows.
const useStore = create(() => initialStates);
const { getState, setState } = useStore;
// usage within function component (reactive)
const Component = () => {
const count = useStore(state => state.count);
// inside some event handler
setState({ count: count + 1 });
};
// usage outside component (non-reactive)
const { count } = getState();
setState({ count: count + 1 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment