rsnext/examples/with-redux-saga
Akira Kijinami d080c8ee79
fix deprecated configureStore in with-redux-saga example (#50342)
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

This PR introduces the following changes:
- Replaced redux's [deprecated
createStore](https://github.com/reduxjs/redux/pull/4336) with
configureStore from @reduxjs/toolkit.
- Updated packages.
- Corrected an error in getStaticProps.

This is my first contribution and there may be some shortcomings, I
appreciate your understanding and look forward to your feedback.

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-06-13 22:00:35 -07:00
..
components Run next/link codemod for Next.js 13 on examples (#41913) 2022-10-30 21:00:45 +01:00
pages fix deprecated configureStore in with-redux-saga example (#50342) 2023-06-13 22:00:35 -07:00
.gitignore Remove incorrect entries for pnpm debug log (#47241) 2023-03-26 22:26:05 -07:00
actions.js Refactor with redux saga (#13342) 2020-05-25 16:21:35 +00:00
package.json fix deprecated configureStore in with-redux-saga example (#50342) 2023-06-13 22:00:35 -07:00
README.md update example Deploy button URLs (#48842) 2023-04-26 13:31:44 -04:00
reducer.js use initial state for rootReducer in with-redux-saga example (#35406) 2022-03-18 11:20:20 +01:00
saga.js enhance-with-redux-saga (#13764) 2020-06-05 14:59:13 +00:00
store.js fix deprecated configureStore in with-redux-saga example (#50342) 2023-06-13 22:00:35 -07:00

redux-saga example

Usually splitting your app state into pages feels natural, but sometimes you'll want to have global state for your app. This is an example using redux and redux-saga that works with universal rendering. This is just one way it can be done. If you have any suggestions or feedback please submit an issue or PR.

This example and documentation is based on the with-redux example.

Deploy your own

Deploy the example using Vercel or preview live with StackBlitz

Deploy with Vercel

How to use

Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:

npx create-next-app --example with-redux-saga with-redux-saga-app
yarn create next-app --example with-redux-saga with-redux-saga-app
pnpm create next-app --example with-redux-saga with-redux-saga-app

Deploy it to the cloud with Vercel (Documentation).

Notes

In the first example we are going to display a digital clock that updates every second. The first render is happening in the server and then the browser will take over. To illustrate this, the server rendered clock will have a different background color than the client one.

Our page is located at pages/index.js so it will map the route /. To get the initial data for rendering we are implementing the static method getInitialProps, initializing the redux store and dispatching the required actions until we are ready to return the initial state to be rendered. Since the component is wrapped with next-redux-wrapper, the component is automatically connected to Redux and wrapped with react-redux Provider, that allows us to access redux state immediately and send the store down to children components so they can access to the state when required.

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.

withRedux function accepts makeStore as first argument, all other arguments are internally passed to react-redux connect() function. makeStore should return a new instance of redux store each time when called, no memoization needed here. See the full example in the Next Redux Wrapper repository.

The trick here for supporting universal redux is to separate the cases for the client and the server. When we are on the server we want to create a new store every time, otherwise different users data will be mixed up. If we are in the client we want to use always the same store and hydrate it when needed. That's what we accomplish in store.js

The clock, under components/clock.js, has access to the state using the connect function from react-redux. In this case Clock is a direct child from the page but it could be deep down the render tree.

The second example, under components/counter.js, shows a simple add counter function with a class component implementing a common redux pattern of mapping state and props. Again, the first render is happening in the server and instead of starting the count at 0, it will dispatch an action in redux that starts the count at 1. This continues to highlight how each navigation triggers a server render first and then a client render second, when you navigate between pages.