我添加了 redux-persist 以便刷新后状态仍然存在。
但是我在终端中不断收到此错误:
✓ 599ms 内编译/发布(3066 个模块) redux-persist 无法创建同步存储。回到 noop 存储。
我的商店文件
import {
configureStore,
combineReducers,
getDefaultMiddleware,
} from '@reduxjs/toolkit';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // Defaults to localStorage for web
import postingsReducer from '@/redux/slices/postingsSlice';
import newPostingFormReducer from '@/redux/slices/newPostingSlice';
import userReducer from '@/redux/slices/userSlice';
const persistConfig = {
key: 'root',
storage,
// Optionally, blacklist any reducers here
};
const rootReducer = combineReducers({
postings: postingsReducer,
newPosting: newPostingFormReducer,
user: userReducer,
});
// Create a persisted reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);
export const store = configureStore({
reducer: persistedReducer,
middleware: getDefaultMiddleware =>
getDefaultMiddleware({
serializableCheck: {
// Ignore persistence-related actions
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
});
export const persistor = persistStore(store);
还有我的主要提供商.tsx
'use client';
import React from 'react';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import { NextUIProvider } from '@nextui-org/react';
import { ThemeProvider as NextThemesProvider } from 'next-themes';
import { store, persistor } from '@/redux/store';
// TODO add full layout skeleon
export default function Providers({ children }: { children: React.ReactNode }) {
return (
<Provider store={store}>
<PersistGate
loading={<div>Providers Loading...</div>}
persistor={persistor}
>
<NextUIProvider>
<NextThemesProvider
attribute="class"
defaultTheme="bounty"
themes={['bitcoin', 'amce', 'bounty']}
>
{children}
</NextThemesProvider>
</NextUIProvider>
</PersistGate>
</Provider>
);
}
我在这个主题上发现的唯一大问题是如何解决:console.error:“redux-persist 无法创建同步存储。回退到“noop”存储
但是我不是在构建 React Native 应用程序,只是使用 NextJS 14 构建 Web 应用程序。
在商店顶部写下
"use client"
'use client'
import {
configureStore,
combineReducers,
getDefaultMiddleware,
} from '@reduxjs/toolkit';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist';