question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lost persist in Dynamic routes

See original GitHub issue

I have integrated your library with redux and redux-persist, everything works fine and persistent data is found when loading and reloading the page, but when reloading on dynamic routes the persistent data is lost. For example, the loading and reloading on “/home” everything works fine, but when “/posts/[id]” persistent data is lost

redux/store.js

import { createStore, compose, applyMiddleware, combineReducers } from 'redux'
import { persistStore } from 'redux-persist'
import { createWrapper } from "next-redux-wrapper";
import thunk from 'redux-thunk'
import { reducers } from 'redux/reducers/index'

const makeStore = () => {
  let store;
  let persistor;

  if (typeof window !== 'undefined') {
    const { persistCombineReducers } = require('redux-persist');
    const storage = require('redux-persist/es/storage').default;

    const config = {
      key: 'root',
      blacklist: ['search', 'database'],
      storage,
    }

    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
    // const composeEnhancers = compose;

    store = createStore(persistCombineReducers(config, reducers), composeEnhancers(
      applyMiddleware(thunk)
    ));

    persistor = persistStore(store)

    store.__persistor = persistor

  }
  else {
    const composeEnhancers = compose;

    store = createStore(combineReducers(reducers), composeEnhancers(
      applyMiddleware(thunk)
    ));

    store.__persistor = null
  }

  return store
}

export const wrapper = createWrapper(makeStore); 

_app.js

import { Provider, useStore } from 'react-redux'
import { PersistGate } from 'redux-persist/integration/react'
import { wrapper } from 'redux/store'

function MyApp(props) {
  const { Component, pageProps } = props

  const store = useStore((state) => state);

  return (
    <React.Fragment>
      <Provider store={store}>
        <PersistGate persistor={store.__persistor} loading={null}>
          <Component {...pageProps} />
        </PersistGate>
      </Provider>
    </React.Fragment>
  );
}

export default wrapper.withRedux(MyApp);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mohdashraf010897commented, Aug 18, 2020

I have found the answer, but thanks for the help

Can you just share it?

1reaction
r3dfearcommented, Aug 18, 2020

I have found the answer, but thanks for the help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic routing gives an error when updating the page Next.js
As I understand it, when updating a page from its route, the id is lost, which is why fetch cannot be executed enter...
Read more >
Page state is not reset for navigating between dynamic routes
Bug report Describe the bug The page state is not reset for navigation between dynamic routes that served by the same source component....
Read more >
The running dynamic route configuration is truncated ... - AskF5
The running dynamic route configuration on your BIG-IP system is truncated and dynamic routing may not function as expected. This issue occurs ...
Read more >
Routing in Next.js – How to Set Up Dynamic Routing with Pre ...
In this tutorial, you'll learn how to set up dynamic routing in Next.js. ... you out on this specific fact that getStaticPaths() is...
Read more >
Shallow Routing - Next.js
Shallow routing allows you to change the URL without running data fetching methods again, that includes getServerSideProps , getStaticProps , and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found