Skip to content

Commit cd34b26

Browse files
Fix readme
1 parent 2060c41 commit cd34b26

File tree

1 file changed

+21
-65
lines changed

1 file changed

+21
-65
lines changed

README.md

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -135,52 +135,27 @@ export const wrapper = createWrapper(makeStore, {debug: true});
135135

136136
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`:
137137

138-
````typescript
138+
```tsx
139139
import React, {FC} from 'react';
140140
import {Provider} from 'react-redux';
141141
import {AppProps} from 'next/app';
142142
import {wrapper} from '../components/store';
143143

144-
const WrappedApp: FC<AppProps> = ({Component, pageProps}) => <Component {...pageProps} />;
145-
146-
Instead of `wrapper.useWrappedStore` you can also use legacy HOC, that can work with class-based components.
147-
148-
: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.
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>
167-
168-
```js
169-
import React from 'react';
170-
import {wrapper} from '../components/store';
171-
172-
const MyApp = ({Component, pageProps}) => <Component {...pageProps} />;
173-
174-
export default wrapper.withRedux(MyApp);
144+
const MyApp: FC<AppProps> = ({Component, ...rest}) => {
145+
const {store, props} = wrapper.useWrappedStore(rest);
146+
return (
147+
<Provider store={store}>
148+
<Component {...props.pageProps} />
149+
</Provider>
150+
);
151+
};
175152
```
176153

177-
</details>
178-
179-
You can also use class-based App wrapper.
154+
Instead of `wrapper.useWrappedStore` you can also use legacy HOC, that can work with class-based components.
180155

181156
: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.
182157

183-
```typescript
158+
```tsx
184159
import React from 'react';
185160
import {wrapper} from '../components/store';
186161
import {AppProps} from 'next/app';
@@ -195,25 +170,6 @@ class MyApp extends React.Component<AppProps> {
195170
export default wrapper.withRedux(MyApp);
196171
```
197172

198-
<details>
199-
<summary>Same code in JavaScript (without types)</summary>
200-
201-
```js
202-
import React from 'react';
203-
import {wrapper} from '../components/store';
204-
205-
class MyApp extends React.Component {
206-
render() {
207-
const {Component, pageProps} = this.props;
208-
return <Component {...pageProps} />;
209-
}
210-
}
211-
212-
export default wrapper.withRedux(MyApp);
213-
```
214-
215-
</details>
216-
217173
## State reconciliation during hydration
218174

219175
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
222178

223179
Another way is to use https://github.com/benjamine/jsondiffpatch to analyze diff and apply it properly:
224180

225-
```js
181+
```tsx
226182
import {HYDRATE} from 'next-redux-wrapper';
227183

228184
// create your reducer
@@ -447,7 +403,7 @@ Stateless function component also can be replaced with class:
447403
```js
448404
class Page extends Component {
449405

450-
public static getInitialProps = wrapper.getInitialPageProps(store => () => { ... });
406+
public static getInitialProps = wrapper.getInitialPageProps(store => () => ({ ... }));
451407

452408
render() {
453409
// stuff
@@ -465,8 +421,8 @@ Although you can wrap individual pages (and not wrap the `pages/_app`) it is not
465421

466422
The wrapper can also be attached to your `_app` component (located in `/pages`). All other components can use the `connect` function of `react-redux`.
467423

468-
```typescript
469-
# pages/_app.tsx
424+
```tsx
425+
// pages/_app.tsx
470426

471427
import React from 'react';
472428
import App, {AppInitialProps} from 'next/app';
@@ -512,8 +468,8 @@ export default wrapper.withRedux(MyApp);
512468
<details>
513469
<summary>Same code in JavaScript (without types)</summary>
514470

515-
```js
516-
# pages/_app.tsx
471+
```jsx
472+
// pages/_app.tsx
517473

518474
import React from 'react';
519475
import App from 'next/app';
@@ -551,7 +507,7 @@ export default wrapper.withRedux(MyApp);
551507

552508
Then all pages can simply be connected (the example considers page components):
553509

554-
```typescript
510+
```tsx
555511
// pages/xxx.tsx
556512

557513
import React from 'react';
@@ -579,7 +535,7 @@ export default connect((state: State) => state)(Page);
579535
<details>
580536
<summary>Same code in JavaScript (without types)</summary>
581537

582-
```js
538+
```jsx
583539
// pages/xxx.js
584540

585541
import React from 'react';
@@ -995,7 +951,7 @@ export const wrapper = createWrapper(makeStore, {debug: true});
995951

996952
Then in the `pages/_app` wait stop saga and wait for it to finish when execution is on server:
997953

998-
```typescript
954+
```tsx
999955
import React from 'react';
1000956
import App, {AppInitialProps} from 'next/app';
1001957
import {END} from 'redux-saga';
@@ -1225,7 +1181,7 @@ export default connect(state => state, {setClientState})(({fromServer, fromClien
12251181

12261182
4. In version `7.x` you have to manually wrap all `getInitialProps` with proper wrappers: `wrapper.getInitialPageProps` and `wrapper.getInitialAppProps`.
12271183

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)
12291185

12301186
## Upgrade from 5.x to 6.x
12311187

0 commit comments

Comments
 (0)