I’m about to setup a new project using nextjs using the new app router, I could set redux toolkit up using a provider based on official example in nextjs repository : https://github.com/vercel/next.js/tree/canary/examples/with-redux
but the problem is when I add redux-persist:
"use client";
import { PersistGate } from "redux-persist/integration/react";
/* Core */
import { Provider } from "react-redux";
/* Instruments */
import { persistor, reduxStore } from "@/lib/redux";
export const Providers = ({ children }) => {
return (
<Provider store={reduxStore}>
<PersistGate persistor={persistor}>
{children}
</PersistGate>
</Provider>
);
};
also tried importing PersistGate using dynamic import:
"use client";
import dynamic from "next/dynamic";
const PersistGateComponent = dynamic(
() =>
import("redux-persist/integration/react").then((mod) => mod.PersistGate),
{
ssr: false,
}
);
const SSROutset = ({ children, persistor }) => {
if (typeof window !== "undefined") {
return (
<PersistGateComponent persistor={persistor}>
{children}
</PersistGateComponent>
);
}
// You can create a visually hidden component to avoid layout flashes and also maintain SEO content
// see Chakra for an example of one https://chakra-ui.com/docs/components/visually-hidden
return (
<span
style={{
border: "0",
clip: "rect(0, 0, 0, 0)",
height: "1px",
width: "1px",
margin: "-1px",
padding: "0",
overflow: "hidden",
whiteSpace: "nowrap",
position: "absolute",
}}
>
{children}
</span>
);
};
export default SSROutset
and:
"use client";
/* Core */
import { Provider } from "react-redux";
/* Instruments */
import { persistor, reduxStore } from "@/lib/redux";
import SSROutset from "./redux/SSROutset";
export const Providers = ({ children }) => {
return (
<Provider store={reduxStore}>
<SSROutset persistor={persistor}>
{children}
</SSROutset>
</Provider>
);
};
still getting following errors:
Hydration failed because the initial UI does not match what was rendered on the server.
2
Answers
I solved this problem with
useEffect
I have work around this problem
the store initial
and after that I make Provider component to wrap the project in Layout
the persist store working but it makes alot of action in the
persist/PERSIST
persist/REYHIDRATION
note: in Provider I put children out of PersistCate because when you add it inside the persist gate all the application will be client side