You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is highly recommended to use `pages/_app` to wrap all pages at once, otherwise due to potential race conditions you may get `Cannot update component while rendering another component`:
:warning: Next.jsprovides [generic`getInitialProps`](https://github.com/vercel/next.js/blob/canary/packages/next/pages/_app.tsx#L21) when using `class MyApp extends App` which will be picked up by wrapper, so you **must not extend `App`** as you'll be opted out of Automatic Static Optimization: https://err.sh/next.js/opt-out-auto-static-optimization. Just export a regular Functional Component as in the example above.
149
-
150
-
```typescript
151
-
import React from 'react';
152
-
import {wrapper} from '../components/store';
153
-
import {AppProps} from 'next/app';
154
-
155
-
class MyApp extends React.Component<AppProps> {
156
-
render() {
157
-
const {Component, pageProps} = this.props;
158
-
return <Component {...pageProps} />;
159
-
}
160
-
}
161
-
162
-
export default wrapper.withRedux(MyApp);
163
-
````
164
-
165
-
<details>
166
-
<summary>Same code in JavaScript (without types)</summary>
Instead of `wrapper.useWrappedStore` you can also use legacy HOC, that can work with class-based components.
180
155
181
156
:warning: Next.js provides [generic `getInitialProps`](https://github.com/vercel/next.js/blob/canary/packages/next/pages/_app.tsx#L21) when using `class MyApp extends App` which will be picked up by wrapper, so you **must not extend `App`** as you'll be opted out of Automatic Static Optimization: https://err.sh/next.js/opt-out-auto-static-optimization. Just export a regular Functional Component as in the example above.
182
157
183
-
```typescript
158
+
```tsx
184
159
importReactfrom'react';
185
160
import {wrapper} from'../components/store';
186
161
import {AppProps} from'next/app';
@@ -195,25 +170,6 @@ class MyApp extends React.Component<AppProps> {
195
170
exportdefaultwrapper.withRedux(MyApp);
196
171
```
197
172
198
-
<details>
199
-
<summary>Same code in JavaScript (without types)</summary>
200
-
201
-
```js
202
-
importReactfrom'react';
203
-
import {wrapper} from'../components/store';
204
-
205
-
classMyAppextendsReact.Component {
206
-
render() {
207
-
const {Component, pageProps} =this.props;
208
-
return <Component {...pageProps} />;
209
-
}
210
-
}
211
-
212
-
exportdefaultwrapper.withRedux(MyApp);
213
-
```
214
-
215
-
</details>
216
-
217
173
## State reconciliation during hydration
218
174
219
175
Each time when pages that have `getStaticProps` or `getServerSideProps` are opened by user the `HYDRATE` action will be dispatched. This may happen during initial page load and during regular page navigation. The `payload` of this action will contain the `state` at the moment of static generation or server side rendering, so your reducer must merge it with existing client state properly.
@@ -222,7 +178,7 @@ Simplest way is to use [server and client state separation](#server-and-client-s
222
178
223
179
Another way is to use https://github.com/benjamine/jsondiffpatch to analyze diff and apply it properly:
224
180
225
-
```js
181
+
```tsx
226
182
import {HYDRATE} from'next-redux-wrapper';
227
183
228
184
// create your reducer
@@ -447,7 +403,7 @@ Stateless function component also can be replaced with class:
4. In version `7.x` you have to manually wrap all `getInitialProps` with proper wrappers: `wrapper.getInitialPageProps` and `wrapper.getInitialAppProps`.
1227
1183
1228
-
5. window.**NEXT_REDUX_WRAPPER_STORE** has been removed as it was causing [issues with hot reloading](https://github.com/kirill-konshin/next-redux-wrapper/pull/324)
1184
+
5.**window.NEXT_REDUX_WRAPPER_STORE** has been removed as it was causing [issues with hot reloading](https://github.com/kirill-konshin/next-redux-wrapper/pull/324)
0 commit comments