-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Comments
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. |
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. |
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 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 |
Thanks a lot for the great examples @sedubois @jonaswindey @MarcoThePoro |
I believe you can achieve that using Next.JS Links |
This adds preliminary support for pages/_app. There's still some work to do here to support things like `App. getInitialProps`, etc., but that will come in later PRs. I've created issues for these for now. One issue remaining is that renaming `pages/_app.js` does not switch to the default _app asset. The server needs to be restarted for this to happen. Similarly, adding an _app.js file does not switch from the default asset to it. I'm guessing there's something wrong with the way I set up the import mappings, and they aren't being invalidated properly? fixes #424
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
The text was updated successfully, but these errors were encountered: