-
I'm following this article https://tkdodo.eu/blog/zustand-and-react-context. here is the code: 'use client';
import { createContext, use, useState } from 'react';
import { SessionValidationResult } from '@repo/auth';
import { createStore, StoreApi, useStore } from 'zustand';
type AuthAction = SessionValidationResult;
const AuthContext = createContext<StoreApi<AuthAction> | null>(null);
export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const [store] = useState(() =>
createStore<AuthAction>((set) => ({
session: null,
user: null,
// actions: {
// increasePopulation: (by) =>
// set((state) => ({ bears: state.bears + by })),
// removeAllBears: () => set({ bears: 0 }),
// },
}))
);
return <AuthContext value={store}>{children}</AuthContext>
};
const useAuthStore = <T,>(selector: (state: AuthAction) => T) => {
const store = use(AuthContext);
if (!store) {
throw new Error('Missing AuthProvider');
}
return useStore(store, selector);
};
export const useSession = () =>
useAuthStore((state) => ({ session: state.session, user: state.user })); |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 13 replies
-
Looks like you use |
Beta Was this translation helpful? Give feedback.
-
Thanks @dai-shi, I didn't realize that🫣 |
Beta Was this translation helpful? Give feedback.
-
Now i got this errors: here is my full code:
'use client';
import { createContext, use, useState } from 'react';
import { SessionValidationResult } from '@repo/auth';
import { createStore, StoreApi, useStore } from 'zustand';
type AuthAction = SessionValidationResult;
const AuthContext = createContext<StoreApi<AuthAction> | null>(null);
export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const [store] = useState(() =>
createStore<AuthAction>(() => ({
session: null,
user: null,
}))
);
return <AuthContext value={store}>{children}</AuthContext>;
};
const useAuthStore = <T,>(selector: (state: AuthAction) => T) => {
const store = use(AuthContext);
if (!store) {
throw new Error('Missing AuthProvider');
}
return useStore(store, selector);
};
export const useSession = () =>
useAuthStore((state) => ({ session: state.session, user: state.user }));
import { AuthProvider } from '@/components/contexts/auth-context';
export default function Layout({ children }: { children: React.ReactNode }) {
return <AuthProvider>{children}</AuthProvider>;
}
import { useSession } from '@/components/contexts/auth-context';
export default function Page() {
const { session, user } = useSession();
console.log({ session, user });
return <div>Page</div>;
} |
Beta Was this translation helpful? Give feedback.
-
i think this is related #1936 |
Beta Was this translation helpful? Give feedback.
-
This issue is fixed if i did this: // error
export const useSession = () => useAuthStore((state) => ({ session: state.session, user: state.user }));
// works
export const useSession = () => useAuthStore((state) => state); is the second one is bad practice in zustand? |
Beta Was this translation helpful? Give feedback.
-
Sorry for the confusion. It was my bad to bring immutability contract in the first place, which has nothing to do with this case actually. So, my point is that useShallow isn't necessary. Consider, const useAuthStore = create((set) => ({
user: { firstName: 'zustand', lastName: 'react' },
// ...
})); In this case, const useUser = () => useAuthStore((state) => state.user); is enough. Adding |
Beta Was this translation helpful? Give feedback.
Looks like you use
.ts
extension. Try changing it to.tsx
.