Next.js
Here are the steps you must take to get Kea set up with Next.js
Install the packages#
In addition to the kea, redux,
react-redux and reselect
packages that Kea normally requires, you must also install the
babel-plugin-kea and
next-redux-wrapper packages:
Set up babel-plugin-kea#
In order to properly hydrate your store between server and client renders, we must install the
babel-plugin-kea package. This ensures that
every kea() call automatically gets a path, which help us link the same logic on the client and
the server.
Add this to your .babelrc:
Create _app.js#
Create pages/_app.js and paste in the following content. The content below is taken from
next-redux-wrapper's readme. Notable changes
have been annotated with comments.
Usage#
Feel free to use kea calls anywhere in your app now!
If you have a logic that initializes the data for a page, pass it as the logic property
on the page's Component: MyPage.logic = logic.
When this logic is built, it'll get a function resolve as part of props. Once your logic
has finished initializing or it crashed, call props.resolve() to signal that the loading has
finished.
Sample app#
Here is the Github API tutorial app, but adapted to work with Next.js server rendering. All the changes are highlighted with a finger. ๐