Last active
April 7, 2024 05:16
-
-
Save ky-zo/bd8fe2e7c19603a6c379771ebaea20e8 to your computer and use it in GitHub Desktop.
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
//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