Last active
May 2, 2020 11:54
-
-
Save Karthik-B-06/279a25578321443a47d3fb3374e0a0be to your computer and use it in GitHub Desktop.
A user profile hook with react-zustand
This file contains hidden or 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 from 'zustand'; | |
| const [userProfileStore] = create(set => ({ | |
| username: '', | |
| email: '', | |
| user_id: '', | |
| token: '', | |
| user_type: '', | |
| isAuthenticated: false, | |
| token: '', | |
| setToken: (token) => set({ | |
| token, | |
| isAuthenticated: true | |
| }), | |
| resetUserProfile: () => set({ | |
| username: '', | |
| email: '', | |
| user_id: '', | |
| token: '', | |
| user_type: '', | |
| isAuthenticated: false, | |
| token: '', | |
| }), | |
| setUserType: (user_type) => set({ | |
| user_type, | |
| }), | |
| setUserId: (user_id) => set({ | |
| user_id, | |
| }), | |
| setUserProfile: ({ username, email, user_id }) => set({ | |
| username, | |
| email, | |
| user_id, | |
| }), | |
| })) | |
| export default userProfileStore; | |
| // The whole store can be used lik | |
| const userProfileStoreData = userProfileStore(); | |
| // The functions can be used like | |
| const setUserProfile = userProfileStore(state => state.setUserProfile); | |
| const setToken = userProfileStore(state => state.setToken); | |
| // The variables can be used like | |
| const isAuthenticated = userProfileStore(state => state.isAuthenticated); | |
| const username = userProfileStore(state => state.isAuthenticated); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment