所以,我使用Nextjs 13.4版本,我创建了一个Redux js,我还使用了redux-persist包来存储数据,下面是我的简单代码:
index.ts:
import { configureStore } from "@reduxjs/toolkit";
import { combineReducers } from "redux";
import thunk from "redux-thunk";
import storage from "redux-persist/lib/storage";
import { persistReducer } from "redux-persist";
import cart from "./cartSlice";
const reducers = combineReducers({ cart });
const config = {
key: "root",
storage,
};
const reducer = persistReducer(config, reducers);
const store = configureStore({
reducer: reducer,
devTools: process.env.NODE_ENV !== "production",
middleware: [thunk],
});
export default store;
字符串
cartSlice.ts:
import { createSlice } from "@reduxjs/toolkit";
export const cartSlice = createSlice({
name: "cart",
initialState: [],
reducers: {},
});
// export const {} = cartSlice.actions;
export default cartSlice.reducer;
型
layout.tsx:
import "./globals.css";
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { Provider } from "react-redux";
import store from "@/store";
import { PersistGate } from "redux-persist/integration/react";
import { persistStore } from "redux-persist";
let persistore = persistStore(store);
const inter = Inter({ subsets: ["latin"] });
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body className={inter.className}>
<Provider store={store}>
<PersistGate loading={null} persistor={persistore}>
{children}
</PersistGate>
</Provider>
</body>
</html>
);
}
型
做了这件事之后,
redux-persist failed to create sync storage. falling back to noop storage.
型
我也得到这个错误:x1c 0d1x不知道为什么我看到这个错误,请给予我修改后的代码.
1条答案
按热度按时间vhmi4jdf1#
store.js
字符串
单独的Providers组件来 Package 我的布局
型
cartSlice.js
型
最后我的layout.js
型
我的坚持工作得很好,我希望你能发现它有帮助。