Skip to content

Instantly share code, notes, and snippets.

@Karthik-B-06
Last active May 2, 2020 11:54
Show Gist options
  • Save Karthik-B-06/279a25578321443a47d3fb3374e0a0be to your computer and use it in GitHub Desktop.
Save Karthik-B-06/279a25578321443a47d3fb3374e0a0be to your computer and use it in GitHub Desktop.
A user profile hook with react-zustand
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