0

i am losing all the state when i call serversideprops with next-redux-wrapper , the problem that when we go to another page the state dont persist , any solution please ? this is the code ( we are using next": "12.2.3", "redux": "^4.2.0", next-redux-wrapper": "^7.0.5")


 import "../styles/globals.css";
import { wrapper } from "../redux/store";

function MyApp({ Component, pageProps }) {
 return <Component {...pageProps} />;
}

export default wrapper.withRedux(MyApp);



const debug = true;

const combinedReducer = combineReducers({
 allHousesReducer,
 userReducer,
});

const bindMiddleware = (middleware) => {
 if (process.env.NODE_ENV !== "production") {
   const { composeWithDevTools } = require("redux-devtools-extension");
   return composeWithDevTools(applyMiddleware(...middleware));
 }
 return applyMiddleware(...middleware);
};



const reducer = (state, action) => {
 if (action.type === HYDRATE) {
   const nextState = {
     ...state, // use previous state
     ...action.payload, // apply delta from hydration
   };
   if (state.allHousesReducer) nextState.allHousesReducer = state.allHousesReducer;
   if (state.userReducer) nextState.userReducer = state.userReducer;

   return nextState;
 } else {
   return combinedReducer(state, action);
 }
};

export const initStore = (context) => createStore(reducer, compose(bindMiddleware([thunkMiddleware, logger])));

export const wrapper = createWrapper(initStore, { debug });

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Mike Owen
  • 1
  • 1
  • `getServerSideProps` runs on the server-side and doesn't have access to the client state. You can initialise your state inside `getServerSideProps` (to be passed to the client) but you cannot access previous state that was setup on the client. – juliomalves Jul 26 '22 at 17:38
  • Thanks a lots for you response , but this is the way that they used it in their official repo 'next-redux-wrapper' , but sadly it'snt work ! – Mike Owen Jul 26 '22 at 17:59
  • @juliomalves please do you have any repo exemple using nextjs and next-redux-wrappe ? thanks – Mike Owen Jul 29 '22 at 16:34
  • Check out the official example: https://github.com/vercel/next.js/tree/canary/examples%2Fwith-redux-wrapper. – juliomalves Jul 29 '22 at 17:53
  • @juliomalves https://github.com/lisamacweb/wrapper this is the repo of my project , thinks alot – Mike Owen Aug 01 '22 at 15:03

0 Answers0