-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update with-redux-thunk example to include HMR (#11816)
* Update with-redux-thunk example to include HMR * Update README * Fix clock component * Fix example component * Fix README
- Loading branch information
1 parent
682120b
commit ffa4089
Showing
12 changed files
with
190 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as types from './types' | ||
|
||
// INITIALIZES CLOCK ON SERVER | ||
export const serverRenderClock = isServer => dispatch => | ||
dispatch({ | ||
type: types.TICK, | ||
payload: { light: !isServer, ts: Date.now() }, | ||
}) | ||
|
||
// INITIALIZES CLOCK ON CLIENT | ||
export const startClock = () => dispatch => | ||
setInterval(() => { | ||
dispatch({ type: types.TICK, payload: { light: true, ts: Date.now() } }) | ||
}, 1000) | ||
|
||
// INCREMENT COUNTER BY 1 | ||
export const incrementCount = () => ({ type: types.INCREMENT }) | ||
|
||
// DECREMENT COUNTER BY 1 | ||
export const decrementCount = () => ({ type: types.DECREMENT }) | ||
|
||
// RESET COUNTER | ||
export const resetCount = () => ({ type: types.RESET }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
export default ({ lastUpdate, light }) => { | ||
return ( | ||
<div className={light ? 'light' : ''}> | ||
{format(new Date(lastUpdate))} | ||
<style jsx>{` | ||
div { | ||
padding: 15px; | ||
display: inline-block; | ||
color: #82fa58; | ||
font: 50px menlo, monaco, monospace; | ||
background-color: #000; | ||
} | ||
import React from 'react' | ||
|
||
.light { | ||
background-color: #999; | ||
} | ||
`}</style> | ||
</div> | ||
) | ||
} | ||
const pad = n => (n < 10 ? `0${n}` : n) | ||
|
||
const format = t => | ||
`${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}` | ||
|
||
const pad = n => (n < 10 ? `0${n}` : n) | ||
const Clock = ({ lastUpdate, light }) => ( | ||
<div className={light ? 'light' : ''}> | ||
{format(new Date(lastUpdate))} | ||
<style jsx>{` | ||
div { | ||
padding: 15px; | ||
display: inline-block; | ||
color: #82fa58; | ||
font: 50px menlo, monaco, monospace; | ||
background-color: #000; | ||
} | ||
.light { | ||
background-color: #999; | ||
} | ||
`}</style> | ||
</div> | ||
) | ||
|
||
export default Clock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import React from 'react' | ||
import { useSelector } from 'react-redux' | ||
import Clock from './clock' | ||
import Counter from './counter' | ||
|
||
export default () => { | ||
const lastUpdate = useSelector(state => state.lastUpdate) | ||
const light = useSelector(state => state.light) | ||
const Examples = () => { | ||
const lastUpdate = useSelector(state => state.timer.lastUpdate) | ||
const light = useSelector(state => state.timer.light) | ||
|
||
return ( | ||
<div> | ||
<div style={{ marginBottom: 10 }}> | ||
<Clock lastUpdate={lastUpdate} light={light} /> | ||
<Counter /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,38 @@ | ||
import React from 'react' | ||
import React, { PureComponent } from 'react' | ||
import { connect } from 'react-redux' | ||
import { startClock, serverRenderClock } from '../store' | ||
import Link from 'next/link' | ||
import { startClock, serverRenderClock } from '../actions' | ||
import Examples from '../components/examples' | ||
|
||
class Index extends React.Component { | ||
static getInitialProps({ reduxStore, req }) { | ||
const isServer = !!req | ||
reduxStore.dispatch(serverRenderClock(isServer)) | ||
class Index extends PureComponent { | ||
static getInitialProps({ store, req }) { | ||
store.dispatch(serverRenderClock(!!req)) | ||
|
||
return {} | ||
} | ||
|
||
componentDidMount() { | ||
const { dispatch } = this.props | ||
this.timer = startClock(dispatch) | ||
this.timer = this.props.startClock() | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.timer) | ||
} | ||
|
||
render() { | ||
return <Examples /> | ||
return ( | ||
<> | ||
<Examples /> | ||
<Link href="/show-redux-state"> | ||
<a>Click to see current Redux State</a> | ||
</Link> | ||
</> | ||
) | ||
} | ||
} | ||
|
||
export default connect()(Index) | ||
const mapDispatchToProps = { | ||
startClock, | ||
} | ||
|
||
export default connect(null, mapDispatchToProps)(Index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react' | ||
import { connect } from 'react-redux' | ||
import Link from 'next/link' | ||
|
||
const codeStyle = { | ||
background: '#ebebeb', | ||
width: 400, | ||
padding: 10, | ||
border: '1px solid grey', | ||
marginBottom: 10, | ||
} | ||
|
||
const ShowReduxState = state => ( | ||
<> | ||
<pre style={codeStyle}> | ||
<code>{JSON.stringify(state, null, 4)}</code> | ||
</pre> | ||
<Link href="/"> | ||
<a>Go Back Home</a> | ||
</Link> | ||
</> | ||
) | ||
|
||
const mapDispatchToProps = state => state | ||
|
||
export default connect(mapDispatchToProps)(ShowReduxState) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { combineReducers } from 'redux' | ||
import * as types from './types' | ||
|
||
// COUNTER REDUCER | ||
const counterReducer = (state = 0, { type }) => { | ||
switch (type) { | ||
case types.INCREMENT: | ||
return state + 1 | ||
case types.DECREMENT: | ||
return state - 1 | ||
case types.RESET: | ||
return 0 | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
// INITIAL TIMER STATE | ||
const initialTimerState = { | ||
lastUpdate: 0, | ||
light: false, | ||
} | ||
|
||
// TIMER REDUCER | ||
const timerReducer = (state = initialTimerState, { type, payload }) => { | ||
switch (type) { | ||
case types.TICK: | ||
return { | ||
lastUpdate: payload.ts, | ||
light: !!payload.light, | ||
} | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
// COMBINED REDUCERS | ||
const reducers = { | ||
counter: counterReducer, | ||
timer: timerReducer, | ||
} | ||
|
||
export default combineReducers(reducers) |
Oops, something went wrong.