README.md

Redux wrapper for Next.js

Usage

npm install next-redux-wrapper --save

Wrapper has to be attached your page components (located in /pages). For safety it is recommended
to wrap all pages, no matter if they use Redux or not, so that you should not care about it anymore in
all child components.

Here is the minimal setup (makeStore and reducer usually are located in other files):

How it works

No magic is involved, it auto-creates Redux store when getInitialProps is called by Next.js and then passes this store
down to React Redux's Provider, which is used to wrap the original component, also automatically. On the client side
it also takes care of using same store every time, whereas on server new store is created for each request.

The withRedux function accepts makeStore as first argument, all other arguments are internally passed to React
Redux's connect() function for simplicity. The makeStore function will receive initial state as one argument and
should return a new instance of redux store each time when called, no memoization needed here, it is automatically done
inside the wrapper.

Async actions in getInitialProps

functionsomeAsyncAction() {
return {
type:'FOO',
payload:newPromise((res) => { res('foo'); })
}
}
functiongetInitialProps({store, isServer, pathname, query}) {
// lets create an action using creatorconstaction=someAsyncAction();
// now the action has to be dispatchedstore.dispatch(action);
// once the payload is available we can resume and render the appreturnaction.payload.then((payload) => {
// you can do something with payload nowreturn {custom:'custom'};
});
}