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

Multiple pages Redux state management - How to? #424

Closed
YarivGilad opened this issue Dec 18, 2016 · 5 comments
Closed

Multiple pages Redux state management - How to? #424

YarivGilad opened this issue Dec 18, 2016 · 5 comments

Comments

@YarivGilad
Copy link

Hi,
I'm trying to figure out how to adjust to the next.js paradigm.
Coming from react-router, you would have an index.js where you would wrap the router with a to inject the redux store, and from there on, each container could reference the store using the Connect method of react-redux.
Since there is no react-router single instance when using next.js, but instead you simply define the main containers in the 'pages' folder - how would you create the store in one place and reference it from all containers?
The redux example on the Next.js Wiki demonstrate a single counter component...
How would you tackle multiple pages needing to reference the same store?
Cheers
Ajar

@YarivGilad YarivGilad changed the title Multiple pages Redux state management Multiple pages Redux state management - How to? Dec 18, 2016
@sedubois
Copy link
Contributor

My Apollo example: https://github.com/relatemind/relate shows one way to share a store between pages (kind of an evolution of the Redux example). You can declare a HOC referenced from every page, and which will take care of providing the store to child components. But I wonder if/how things could be improved now that #301 is available? Haven't looked into that yet. Also see ongoing discussion in #387.

@jonaswindey
Copy link
Contributor

You can also take the approach of a 'global' layout like described here: https://github.com/zeit/next.js/wiki/Global-styles-and-layouts

import React from 'react'
import Page from '../layouts/main'
export default () => (
  <Page>
    <p>my page with global styles!</p>
  </Page>
)

That way you can have a shared 'Page' between all your pages, and put your central store in there.
Same can be reached with a HOC by returning your component wrapped in a function.

@MarcPorciuncula
Copy link

If you look at the store code in the redux example wiki page it actually puts the store on a global variable. If you transition to a new page and call initStore it just returns the existing one so the store is persisted across pages.

export const initStore = (reducer, initialState, isServer) => {
  if (isServer && typeof window === 'undefined') {
    return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
  } else {
    if (!window.store) {
      window.store = createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
    }
    return window.store
  } 
}

You just need to render a <Provider> at the root of each page and pass in that store.

@rauchg
Copy link
Member

rauchg commented Dec 19, 2016

Thanks a lot for the great examples @sedubois @jonaswindey @MarcoThePoro

@rauchg rauchg closed this as completed Dec 19, 2016
@ops-gaurav
Copy link

I believe you can achieve that using Next.JS Links

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

6 participants