1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

updating with-redux-saga example (#4356)

This commit is contained in:
Gaston Fartek 2018-05-13 12:26:17 -04:00 committed by Tim Neutkens
parent af1082286e
commit 4f59c95149
5 changed files with 54 additions and 22 deletions

View file

@ -10,14 +10,14 @@
"dependencies": { "dependencies": {
"es6-promise": "4.1.1", "es6-promise": "4.1.1",
"isomorphic-unfetch": "2.0.0", "isomorphic-unfetch": "2.0.0",
"next": "latest", "next": "6.0.1",
"next-redux-saga": "1.0.1", "next-redux-saga": "3.0.0-beta.1",
"next-redux-wrapper": "1.2.0", "next-redux-wrapper": "2.0.0-beta.6",
"react": "^16.0.0", "react": "^16.0.0",
"react-dom": "^16.0.0", "react-dom": "^16.0.0",
"react-redux": "5.0.5", "react-redux": "5.0.7",
"redux": "3.7.2", "redux": "4.0.0",
"redux-saga": "0.15.4" "redux-saga": "0.16.0"
}, },
"devDependencies": { "devDependencies": {
"redux-devtools-extension": "2.13.2" "redux-devtools-extension": "2.13.2"

View file

@ -0,0 +1,31 @@
import App, { Container } from 'next/app'
import React from 'react'
import { Provider } from 'react-redux'
import withRedux from 'next-redux-wrapper'
import createStore from '../store'
import withReduxSaga from 'next-redux-saga'
class MyApp extends App {
static async getInitialProps ({ Component, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps({ ctx })
}
return { pageProps }
}
render () {
const { Component, pageProps, store } = this.props
return (
<Container>
<Provider store={store}>
<Component {...pageProps} />
</Provider>
</Container>
)
}
}
export default withRedux(createStore)(withReduxSaga({ async: true })(MyApp))

View file

@ -1,13 +1,14 @@
import React from 'react' import React from 'react'
import {connect} from 'react-redux'
import {increment, loadData, startClock, tickClock} from '../actions' import {increment, loadData, startClock, tickClock} from '../actions'
import {withReduxSaga} from '../store'
import Page from '../components/page' import Page from '../components/page'
class Counter extends React.Component { class Counter extends React.Component {
static async getInitialProps ({store, isServer}) { static async getInitialProps (props) {
store.dispatch(tickClock(isServer)) const { store } = props.ctx
store.dispatch(tickClock(props.isServer))
store.dispatch(increment()) store.dispatch(increment())
if (!store.getState().placeholderData) { if (!store.getState().placeholderData) {
store.dispatch(loadData()) store.dispatch(loadData())
} }
@ -22,4 +23,4 @@ class Counter extends React.Component {
} }
} }
export default withReduxSaga(Counter) export default connect()(Counter)

View file

@ -1,13 +1,14 @@
import React from 'react' import React from 'react'
import {connect} from 'react-redux'
import {increment, startClock, tickClock} from '../actions' import {increment, startClock, tickClock} from '../actions'
import {withReduxSaga} from '../store'
import Page from '../components/page' import Page from '../components/page'
class Counter extends React.Component { class Counter extends React.Component {
static async getInitialProps ({store, isServer}) { static async getInitialProps (props) {
const { store, isServer } = props.ctx
store.dispatch(tickClock(isServer)) store.dispatch(tickClock(isServer))
store.dispatch(increment()) store.dispatch(increment())
return { isServer }
} }
componentDidMount () { componentDidMount () {
@ -19,4 +20,4 @@ class Counter extends React.Component {
} }
} }
export default withReduxSaga(Counter) export default connect()(Counter)

View file

@ -1,8 +1,5 @@
import {createStore, applyMiddleware} from 'redux' import {createStore, applyMiddleware} from 'redux'
import withRedux from 'next-redux-wrapper'
import nextReduxSaga from 'next-redux-saga'
import createSagaMiddleware from 'redux-saga' import createSagaMiddleware from 'redux-saga'
import rootReducer, {exampleInitialState} from './reducer' import rootReducer, {exampleInitialState} from './reducer'
import rootSaga from './saga' import rootSaga from './saga'
@ -23,10 +20,12 @@ export function configureStore (initialState = exampleInitialState) {
bindMiddleware([sagaMiddleware]) bindMiddleware([sagaMiddleware])
) )
store.sagaTask = sagaMiddleware.run(rootSaga) store.runSagaTask = () => {
store.sagaTask = sagaMiddleware.run(rootSaga)
}
store.runSagaTask()
return store return store
} }
export function withReduxSaga (BaseComponent) { export default configureStore
return withRedux(configureStore)(nextReduxSaga(BaseComponent))
}