Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting React state between route navigation #590

Closed
viankakrisna opened this issue Dec 31, 2016 · 23 comments
Closed

Persisting React state between route navigation #590

viankakrisna opened this issue Dec 31, 2016 · 23 comments

Comments

@viankakrisna
Copy link

viankakrisna commented Dec 31, 2016

I have followed the redux example, and rendering an iframe inside a shared component. but it seems that the Provider component always rerender the app every time the route changes and triggers the iframe to reload. I want it to persist. Is it possible?

@arunoda arunoda changed the title Persisting state between route navigation Persisting React state between route navigation Jan 1, 2017
@arunoda
Copy link
Contributor

arunoda commented Jan 1, 2017

Currently it's kind a hard to have this.
We only persist module state only.
When we are switching pages, it'll render new React components (pages).
So, that's why we see that.

Currently, I hope we could do something with a custom document.

@shelldandy
Copy link

Maybe save the current state in localstorage and if it exists load it from there on the next page?

@arunoda
Copy link
Contributor

arunoda commented Jan 1, 2017

@Mike3run That's not what @viankakrisna is asking.
We have JS module level persistence. So no need to use local storage.

But @viankakrisna is asking about the React state. Because of that, his iframe inside the common component getting re-rendered again.

@viankakrisna
Copy link
Author

viankakrisna commented Jan 1, 2017

@arunoda I'm testing your suggestion with custom document:

    <html>
      <Head>
        <link rel='stylesheet' href='/static/style.css' />
      </Head>
      <body>
        <Provider store={this.store}>
          <div>
            <Navbar />
            <YouTubePlayer />
            <Main />
          </div>
        </Provider>
        <NextScript />
      </body>
    </html>

But getting this error:

Invariant Violation: Could not find "store" in either the context or props of "Connect(Component)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Component)".

It seems that the pages cannot see the Provider.

@rauchg
Copy link
Member

rauchg commented Jan 1, 2017

Are you sure he's asking about React state vs a re-usable global store ?

@arunoda
Copy link
Contributor

arunoda commented Jan 1, 2017

@rauchg I figured it based on this comment:

rerender the app every time the route changes and triggers the iframe to reload.

Anyway, @viankakrisna could you send us a sample repo. May be we could think something to do.

@viankakrisna
Copy link
Author

@rauchbg yes, i'm talking about react state. Sorry if it's not clear enough. @arunoda, will do when i get home. Like you said, I think the issue is it always renders new React component on route changes. Can we treat the pages as the children of the Document? So the main shell shouldn't be recreated every route changes.

ps: sorry for my english, not a native :(

@arunoda
Copy link
Contributor

arunoda commented Jan 1, 2017

@viankakrisna let's see what we can do with the example.

@timneutkens
Copy link
Member

Closing because of inactivity.

@adarshsingh1407
Copy link

Next.js is great for SSR and code-splitting. It's on path to be an amazing framework.

But I had the same issue as @viankakrisna while making an audio player webapp.
I want my AudioComponent which plays music, to maintain its state, while the user browses through the genres and artists to add them to his playlist.

It would be great if you guys could do something about it.

For reference, here is the github repo the player app :
nextjs-audio-player

@arunoda
Copy link
Contributor

arunoda commented Jun 20, 2017

@adarshsingh1407 for now you can do this by wrapping all these functionalities into a single page and use our custom routing API to do custom URLs.

@bookercodes
Copy link

@arunoda So I would have one page that conditionally renders a view from render? Something like,

render () {
   if (condition) {
     return this.renderThis()
   } else {
      return this.renderThat()
   }
}

?

Can you please elaborate on what you mean by custom API routing and how we can accomplish that with Next?

@d4ny0
Copy link

d4ny0 commented Nov 30, 2017

do you have a solution for this problem using a custom express server?
i use the routing like this:
server.js:

const webRoutes = require('./app/routes/web')(server, app);
server.get('*', (req, res) => handle(req, res));

routes.js:

// import controller middlewares
const authController = require('../controllers/authController');

// define the actual page files for each route
const render = {
	index: app => (req, res) => app.render(req, res, '/index'),
	create: app => (req, res) => app.render(req, res, '/assignment/create'),
	preview: app => (req, res) => app.render(req, res, '/assignment/preview'),
};

// link url to pages, apply middlewares where needed
module.exports = (server, app) => {
	server.get('/dashboard', authController.login, render.index(app));
	server.get('/create', render.create(app));
	server.get('/create/assignment', render.create(app));
	server.get('/preview/assignment', render.preview(app));
};

routing this way i can apply express middlewares to my routes. but everytime i load a new page my redux state is gone

@glemiere
Copy link

glemiere commented Apr 5, 2018

Heeey!

@timneutkens this might be interesting to add to your checklist for v6 ;)

@hakimelek
Copy link

I believe that this should work now using custom App.js component.

@yareyaredesuyo
Copy link

example exists?

@johansedgeware
Copy link

So have I got it right if I'd say that this isn't possible as of yet?

@alizamani
Copy link

Next.js is great for SSR and code-splitting. It's on path to be an amazing framework.

But I had the same issue as @viankakrisna while making an audio player webapp.
I want my AudioComponent which plays music, to maintain its state, while the user browses through the genres and artists to add them to his playlist.

It would be great if you guys could do something about it.

For reference, here is the github repo the player app :
nextjs-audio-player

@adarshsingh1407 Did you reach a consolation to continue playing music while navigating between pages?

@GusRuss89
Copy link

I came up with a solution. It has some issues but does work. Here's a gist - https://gist.github.com/GusRuss89/df05ea25310043fc38a5e2ba3cb0c016

@acusti
Copy link

acusti commented Mar 18, 2021

if you use a custom App component as described here and track your state / render your providers from that component, the state will persist across route navigation, because the App component will stay rendered and not be unmounted throughout those route changes. to address your question directly, @johansedgeware:

So have I got it right if I'd say that this isn't possible as of yet?

it is possible using the custom App component and initializing / maintaining the state within the context of that component.

@MagicMikeChen
Copy link

I solved similar problem with this:

https://nextjs.org/docs/basic-features/layouts

Just make a HOC wrapped the whole _app and set what you want to do in the HOC.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests