Last active
October 25, 2020 06:22
-
-
Save Munawwar/c090f66ad7b084c718739b9cc6d07cfc to your computer and use it in GitHub Desktop.
Zustand state manager usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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