Commit b90c77b1 authored by yhirano55's avatar yhirano55 Committed by Tim Neutkens

Improve with-redux-saga example (#4392)

* Rename page component's class name: Counter => Index, Counter => Other
* Rename counter component class name: AddCount => Counter
* Add counter actions `decrement` and `reset` same as with-redux example
* Modify page link by NavigateTo attr in Page component
* Modify license MIT => ISC same as others in package.json
* Modify README
parent e8c7dd49
...@@ -61,7 +61,7 @@ The trick here for supporting universal redux is to separate the cases for the c ...@@ -61,7 +61,7 @@ The trick here for supporting universal redux is to separate the cases for the c
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 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/add-count.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. The second example, under `components/counter.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.
## What changed with next-redux-saga ## What changed with next-redux-saga
......
export const actionTypes = { export const actionTypes = {
FAILURE: 'FAILURE', FAILURE: 'FAILURE',
INCREMENT: 'INCREMENT', INCREMENT: 'INCREMENT',
DECREMENT: 'DECREMENT',
RESET: 'RESET',
LOAD_DATA: 'LOAD_DATA', LOAD_DATA: 'LOAD_DATA',
LOAD_DATA_SUCCESS: 'LOAD_DATA_SUCCESS', LOAD_DATA_SUCCESS: 'LOAD_DATA_SUCCESS',
START_CLOCK: 'START_CLOCK', START_CLOCK: 'START_CLOCK',
...@@ -18,6 +20,14 @@ export function increment () { ...@@ -18,6 +20,14 @@ export function increment () {
return {type: actionTypes.INCREMENT} return {type: actionTypes.INCREMENT}
} }
export function decrement () {
return {type: actionTypes.DECREMENT}
}
export function reset () {
return {type: actionTypes.RESET}
}
export function loadData () { export function loadData () {
return {type: actionTypes.LOAD_DATA} return {type: actionTypes.LOAD_DATA}
} }
......
import React, {Component} from 'react' import React, {Component} from 'react'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {increment} from '../actions' import {increment, decrement, reset} from '../actions'
class AddCount extends Component { class Counter extends Component {
add = () => { increment = () => {
this.props.dispatch(increment()) this.props.dispatch(increment())
} }
decrement = () => {
this.props.dispatch(decrement())
}
reset = () => {
this.props.dispatch(reset())
}
render () { render () {
const {count} = this.props const {count} = this.props
return ( return (
...@@ -18,13 +26,15 @@ class AddCount extends Component { ...@@ -18,13 +26,15 @@ class AddCount extends Component {
} }
`}</style> `}</style>
<h1> <h1>
AddCount: <span>{count}</span> Count: <span>{count}</span>
</h1> </h1>
<button onClick={this.add}>Add To Count</button> <button onClick={this.increment}>+1</button>
<button onClick={this.decrement}>-1</button>
<button onClick={this.reset}>Reset</button>
</div> </div>
) )
} }
} }
const mapStateToProps = ({count}) => ({count}) const mapStateToProps = ({count}) => ({count})
export default connect(mapStateToProps)(AddCount) export default connect(mapStateToProps)(Counter)
import Link from 'next/link' import Link from 'next/link'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import AddCount from './add-count' import Counter from './counter'
import Clock from './clock' import Clock from './clock'
function Page ({error, lastUpdate, light, linkTo, placeholderData, title}) { function Page ({error, lastUpdate, light, linkTo, NavigateTo, placeholderData, title}) {
return ( return (
<div> <div>
<h1> <h1>
{title} {title}
</h1> </h1>
<Clock lastUpdate={lastUpdate} light={light} /> <Clock lastUpdate={lastUpdate} light={light} />
<AddCount /> <Counter />
<nav> <nav>
<Link href={linkTo}> <Link href={linkTo}>
<a>Navigate</a> <a>Navigate: {NavigateTo}</a>
</Link> </Link>
</nav> </nav>
{placeholderData && {placeholderData &&
......
{ {
"name": "with-redux-saga", "name": "with-redux-saga",
"version": "1.0.0", "version": "1.0.0",
"license": "MIT", "license": "ISC",
"scripts": { "scripts": {
"dev": "next", "dev": "next",
"build": "next build", "build": "next build",
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"dependencies": { "dependencies": {
"es6-promise": "4.1.1", "es6-promise": "4.1.1",
"isomorphic-unfetch": "2.0.0", "isomorphic-unfetch": "2.0.0",
"next": "6.0.1", "next": "latest",
"next-redux-saga": "3.0.0-beta.1", "next-redux-saga": "3.0.0-beta.1",
"next-redux-wrapper": "2.0.0-beta.6", "next-redux-wrapper": "2.0.0-beta.6",
"react": "^16.0.0", "react": "^16.0.0",
......
...@@ -2,9 +2,10 @@ import App, { Container } from 'next/app' ...@@ -2,9 +2,10 @@ import App, { Container } from 'next/app'
import React from 'react' import React from 'react'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import withRedux from 'next-redux-wrapper' import withRedux from 'next-redux-wrapper'
import createStore from '../store'
import withReduxSaga from 'next-redux-saga' import withReduxSaga from 'next-redux-saga'
import createStore from '../store'
class MyApp extends App { class MyApp extends App {
static async getInitialProps ({ Component, ctx }) { static async getInitialProps ({ Component, ctx }) {
let pageProps = {} let pageProps = {}
......
import React from 'react' import React from 'react'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {increment, loadData, startClock, tickClock} from '../actions'
import {loadData, startClock, tickClock} from '../actions'
import Page from '../components/page' import Page from '../components/page'
class Counter extends React.Component { class Index extends React.Component {
static async getInitialProps (props) { static async getInitialProps (props) {
const { store } = props.ctx const { store, isServer } = props.ctx
store.dispatch(tickClock(props.isServer)) store.dispatch(tickClock(isServer))
store.dispatch(increment())
if (!store.getState().placeholderData) { if (!store.getState().placeholderData) {
store.dispatch(loadData()) store.dispatch(loadData())
} }
return { isServer }
} }
componentDidMount () { componentDidMount () {
...@@ -19,8 +21,8 @@ class Counter extends React.Component { ...@@ -19,8 +21,8 @@ class Counter extends React.Component {
} }
render () { render () {
return <Page title='Index Page' linkTo='/other' /> return <Page title='Index Page' linkTo='/other' NavigateTo='Other Page' />
} }
} }
export default connect()(Counter) export default connect()(Index)
import React from 'react' import React from 'react'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {increment, startClock, tickClock} from '../actions'
import {startClock, tickClock} from '../actions'
import Page from '../components/page' import Page from '../components/page'
class Counter extends React.Component { class Other extends React.Component {
static async getInitialProps (props) { static async getInitialProps (props) {
const { store, isServer } = props.ctx const { store, isServer } = props.ctx
store.dispatch(tickClock(isServer)) store.dispatch(tickClock(isServer))
store.dispatch(increment())
return { isServer } return { isServer }
} }
...@@ -16,8 +16,8 @@ class Counter extends React.Component { ...@@ -16,8 +16,8 @@ class Counter extends React.Component {
} }
render () { render () {
return <Page title='Other Page' linkTo='/' /> return <Page title='Other Page' linkTo='/' NavigateTo='Index Page' />
} }
} }
export default connect()(Counter) export default connect()(Other)
...@@ -22,6 +22,18 @@ function reducer (state = exampleInitialState, action) { ...@@ -22,6 +22,18 @@ function reducer (state = exampleInitialState, action) {
...{count: state.count + 1} ...{count: state.count + 1}
} }
case actionTypes.DECREMENT:
return {
...state,
...{count: state.count - 1}
}
case actionTypes.RESET:
return {
...state,
...{count: exampleInitialState.count}
}
case actionTypes.LOAD_DATA_SUCCESS: case actionTypes.LOAD_DATA_SUCCESS:
return { return {
...state, ...state,
......
import {createStore, applyMiddleware} from 'redux' import {createStore, applyMiddleware} from 'redux'
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'
...@@ -13,7 +14,7 @@ const bindMiddleware = (middleware) => { ...@@ -13,7 +14,7 @@ const bindMiddleware = (middleware) => {
return applyMiddleware(...middleware) return applyMiddleware(...middleware)
} }
export function configureStore (initialState = exampleInitialState) { function configureStore (initialState = exampleInitialState) {
const store = createStore( const store = createStore(
rootReducer, rootReducer,
initialState, initialState,
......
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