skip to Main Content

I’ve added redux-persist so that the state persists after refresh.

But I keep getting this error in the terminal:

✓ Compiled /postings in 599ms (3066 modules)
redux-persist failed to create sync storage. falling back to noop storage.

My store file

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);

And my main providers.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>
  );
}

The only big question I found on this topic was How to solve: console.error: "redux-persist failed to create sync storage. falling back to "noop" storage

However I’m not building a React Native app, just a web app with NextJS 14.

2

Answers


  1. Write "use client" on the top of the store

    'use client'
    
    import {
      configureStore,
      combineReducers,
      getDefaultMiddleware,
    } from '@reduxjs/toolkit';
    import {
      persistStore,
      persistReducer,
      FLUSH,
      REHYDRATE,
      PAUSE,
      PERSIST,
      PURGE,
      REGISTER,
    } from 'redux-persist';
    
    Login or Signup to reply.
  2. Next.js defaults to SSR (Server-Side Rendering).
    localStorage only only works in Client Components.
    Add the "use client" directive at the top of the file to use it.

    like:

    "use client"
    
    import {
      configureStore,
      combineReducers,
      getDefaultMiddleware,
    } from '@reduxjs/toolkit';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search