Unverified Commit b8284d9a authored by Kirill Konshin's avatar Kirill Konshin Committed by GitHub

Upgrade Next Redux Wrapper to version 6 in examples (#11802)

Fixes https://github.com/zeit/next.js/issues/12920
parent ccea1c01
......@@ -51,7 +51,7 @@ Our page is located at `pages/index.js` so it will map the route `/`. To get the
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` function will receive initialState as one argument and should return a new instance of redux store each time when called, no memoization needed here. See the [full example](https://github.com/kirill-konshin/next-redux-wrapper#usage) in the Next Redux Wrapper repository. And there's another package [next-connect-redux](https://github.com/huzidaha/next-connect-redux) available with similar features.
This example wraps pages individually using `getStaticProps` and `getServerSideProps`. See the [full example](https://github.com/kirill-konshin/next-redux-wrapper#usage) 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.
......
......@@ -7,8 +7,8 @@
"start": "next start"
},
"dependencies": {
"next": "latest",
"next-redux-wrapper": "latest",
"next": "9.4.1",
"next-redux-wrapper": "^6.0.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "7.1.3",
......
import App from 'next/app'
import React from 'react'
import { Provider } from 'react-redux'
import withRedux from 'next-redux-wrapper'
import { initStore } from '../store/store'
import { wrapper } from '../store/store'
const MyApp = (props) => {
const { Component, pageProps, store } = props
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
class WrappedApp extends App {
render() {
const { Component, pageProps } = this.props
return <Component {...pageProps} />
}
}
export default withRedux(initStore)(MyApp)
export default wrapper.withRedux(WrappedApp)
import React, { useEffect } from 'react'
import { bindActionCreators } from 'redux'
import { addCount } from '../store/count/action'
import { startClock, serverRenderClock } from '../store/tick/action'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import Page from '../components/Page'
import { addCount } from '../store/count/action'
import { wrapper } from '../store/store'
import { serverRenderClock, startClock } from '../store/tick/action'
const Counter = (props) => {
const Index = (props) => {
useEffect(() => {
const timer = props.startClock()
......@@ -17,11 +18,10 @@ const Counter = (props) => {
return <Page title="Index Page" linkTo="/other" />
}
Counter.getInitialProps = async ({ store, isServer }) => {
store.dispatch(serverRenderClock(isServer))
export const getStaticProps = wrapper.getStaticProps(async ({ store }) => {
store.dispatch(serverRenderClock(true))
store.dispatch(addCount())
return { isServer }
}
})
const mapDispatchToProps = (dispatch) => {
return {
......@@ -30,4 +30,4 @@ const mapDispatchToProps = (dispatch) => {
}
}
export default connect(null, mapDispatchToProps)(Counter)
export default connect(null, mapDispatchToProps)(Index)
import React, { useEffect } from 'react'
import { bindActionCreators } from 'redux'
import { addCount } from '../store/count/action'
import { startClock, serverRenderClock } from '../store/tick/action'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import Page from '../components/Page'
import { addCount } from '../store/count/action'
import { wrapper } from '../store/store'
import { serverRenderClock, startClock } from '../store/tick/action'
const Counter = (props) => {
const Other = (props) => {
useEffect(() => {
const timer = props.startClock()
......@@ -17,11 +18,12 @@ const Counter = (props) => {
return <Page title="Other Page" linkTo="/" />
}
Counter.getInitialProps = async ({ store, isServer }) => {
store.dispatch(serverRenderClock(isServer))
store.dispatch(addCount())
return { isServer }
}
export const getServerSideProps = wrapper.getServerSideProps(
async ({ store }) => {
store.dispatch(serverRenderClock(true))
store.dispatch(addCount())
}
)
const mapDispatchToProps = (dispatch) => {
return {
......@@ -30,4 +32,4 @@ const mapDispatchToProps = (dispatch) => {
}
}
export default connect(null, mapDispatchToProps)(Counter)
export default connect(null, mapDispatchToProps)(Other)
import { createStore, applyMiddleware, combineReducers } from 'redux'
import { HYDRATE, createWrapper } from 'next-redux-wrapper'
import thunkMiddleware from 'redux-thunk'
import count from './count/reducer'
import tick from './tick/reducer'
......@@ -11,12 +12,26 @@ const bindMiddleware = (middleware) => {
return applyMiddleware(...middleware)
}
export const initStore = () => {
return createStore(
combineReducers({
count,
tick,
}),
bindMiddleware([thunkMiddleware])
)
const combinedReducer = combineReducers({
count,
tick,
})
const reducer = (state, action) => {
if (action.type === HYDRATE) {
const nextState = {
...state, // use previous state
...action.payload, // apply delta from hydration
}
if (state.count) nextState.count = state.count // preserve count value on client side navigation
return nextState
} else {
return combinedReducer(state, action)
}
}
const initStore = () => {
return createStore(reducer, bindMiddleware([thunkMiddleware]))
}
export const wrapper = createWrapper(initStore)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment