rsnext/examples/with-redux-wrapper
Mohan Raj Rajamanickam fb10b2cfb1
chore(examples): relax pinned versions to fix npm install fail due to peer deps issue (#51636)
Currently `npm install` fails with
```
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree npm ERR!
npm ERR! While resolving: undefined@undefined
npm ERR! Found: react@18.2.0
npm ERR! node_modules/react
npm ERR!   react@"^18.2.0" from the root project
npm ERR!   peer react@"*" from next-redux-wrapper@7.0.5
npm ERR!   node_modules/next-redux-wrapper
npm ERR!     next-redux-wrapper@"^7.0.2" from the root project
npm ERR!   2 more (next, react-dom)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.3" from react-redux@7.1.3
npm ERR! node_modules/react-redux
npm ERR!   react-redux@"7.1.3" from the root project
npm ERR!   peer react-redux@"*" from next-redux-wrapper@7.0.5
npm ERR!   node_modules/next-redux-wrapper
npm ERR!     next-redux-wrapper@"^7.0.2" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
```

<!-- 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

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating 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 #

-->

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-06-24 00:12:38 -07:00
..
components Run next/link codemod for Next.js 13 on examples (#41913) 2022-10-30 21:00:45 +01:00
pages upgrade next-redux-wrapper to 7.0.2 (#26521) 2021-07-16 12:28:31 +00:00
store fix(examples/with-redux-wrapper): wrong initial state (close #17299) (#17335) 2020-09-24 19:35:46 +00:00
.gitignore Remove incorrect entries for pnpm debug log (#47241) 2023-03-26 22:26:05 -07:00
.npmrc Add with redux wrapper hooks (#9954) 2020-01-09 10:31:49 -05:00
package.json chore(examples): relax pinned versions to fix npm install fail due to peer deps issue (#51636) 2023-06-24 00:12:38 -07:00
README.md update example Deploy button URLs (#48842) 2023-04-26 13:31:44 -04:00

With Redux Wrapper 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 on how you can use redux that also works with our universal rendering approach. This is just a way you can do it but it's not the only one.

Deploy your own

Deploy the example using Vercel:

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-wrapper with-redux-wrapper-app
yarn create next-app --example with-redux-wrapper with-redux-wrapper-app
pnpm create next-app --example with-redux-wrapper with-redux-wrapper-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.

This example wraps pages individually using getStaticProps and getServerSideProps. See the full example in the Next Redux Wrapper repository, you can also opt-in to use App.getInitialProps and Page.getInitialProps as before.

To pass the initial state from the server to the client we pass it as a prop called initialState so then it's available when the client takes over.

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. That's what we accomplish on 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/AddCount.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.