Skip to content

Instantly share code, notes, and snippets.

@ky-zo
Last active April 7, 2024 05:16
Show Gist options
  • Save ky-zo/bd8fe2e7c19603a6c379771ebaea20e8 to your computer and use it in GitHub Desktop.
Save ky-zo/bd8fe2e7c19603a6c379771ebaea20e8 to your computer and use it in GitHub Desktop.
//Correct way of creating hooks as a facade layer
export const useSettings = () => {
const settings = useStore((state) => state.settings)
const setSettings = useStore((state) => state.setSettings)
return { settings, setSettings }
}
//Incorrect way of creating a hook: this causes the zustand state to subscribte to the whole store, which is not necessary.
//It compares OBJECT to OBJECT, which will always be different (as Objects are compared by the memory address).
//Returning functions one by one like in the first hook is correct, as it does not cause the whole store to be subscribed.
export const useSettings = () => {
return useStore((state) => {
return { settings: state.settings, setSettings: state.setSettings }
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment