25 lines
804 B
TypeScript
25 lines
804 B
TypeScript
// app/redux/ReduxProvider.tsx
|
|
"use client";
|
|
|
|
import React, { useEffect } from "react"; // Import useEffect
|
|
import { Provider } from "react-redux";
|
|
import { store } from "./store";
|
|
import { initializeAuth } from "./auth/authSlice";
|
|
|
|
export default function ReduxProvider({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
// Get the dispatch function directly from the store for initial dispatch
|
|
const dispatch = store.dispatch;
|
|
|
|
useEffect(() => {
|
|
// Dispatch initializeAuth when the ReduxProvider component mounts on the client.
|
|
// This ensures your Redux isLoggedIn state is synced with localStorage after a page refresh.
|
|
dispatch(initializeAuth());
|
|
}, [dispatch]); // Dependency array ensures it runs only once on mount
|
|
|
|
return <Provider store={store}>{children}</Provider>;
|
|
}
|