redux persist in the next js project persist:root is not creating localstorage

…衆ロ難τιáo~ 提交于 2020-12-15 06:27:49

问题


using the nextjs framework the redux-persist is not creating the local storage values. After login the persist:root is not showing in the local

In the reactjs framework, I have tried the redux persist the persist:root created in the local storage but in the nextjs framework the same method I am following the errors not coming but the persist:root is not showing
in the local storage

//store.js

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import reducers from './reducers';
import { persistReducer } from 'redux-persist';
import nextConnectRedux from 'next-connect-redux';
import storage from 'redux-persist/lib/storage/session';

const persistConfig = {
    key: "root",
    storage: storage,
}
const persistedReducer = persistReducer(persistConfig, reducers)

const middleware = [thunk];

const composeEnhancers =
  typeof window !== 'undefined' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    }) : compose;

const enhancer = composeEnhancers(
        applyMiddleware(...middleware),
    );

const store = () => {
    return createStore(
    persistedReducer,
    {},
    enhancer
)};

const nextConnect = nextConnectRedux(store)

export default nextConnect; 

// index.js

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import { persistStore } from 'redux-persist';
import nextConnect from '../store';
import Route from '../routes';
import { BrowserRouter } from "react-router-dom";

class App extends Component {
  render () {
    const persistor = persistStore(nextConnect);
    return (
        <Provider store={nextConnect}>
        <PersistGate loading={null} persistor={persistor}>
          <BrowserRouter>
           <Route/>
          </BrowserRouter>
        </PersistGate>
        </Provider>
    )
  }
}

export default App;

I want the persist:root in the local storage to be showed


回答1:


if you are rendering React on the server, you cant use redux-persist with default configs When you are using redux-persist with the default configuration, means you are storing your data on browser storage and you don’t have a browser on the server The following implementation shows you how to integrate Redux Persist into Next.js

rootReducer.js

store.js

_app.js

That's it

resource Redux Persist with Next.js



来源:https://stackoverflow.com/questions/56730335/redux-persist-in-the-next-js-project-persistroot-is-not-creating-localstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!