Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routing on the client with Redux #691

Merged
merged 2 commits into from
Jan 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions examples/with-redux/components/Clock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import React from 'react'
import { connect } from 'react-redux'

export default connect(state => state)(({ lastUpdate, light }) => {
export default ({ lastUpdate, light }) => {
return (
<div className={light ? 'light' : ''}>
{format(new Date(lastUpdate))}
Expand All @@ -20,7 +17,7 @@ export default connect(state => state)(({ lastUpdate, light }) => {
`}</style>
</div>
)
})
}

const format = t => `${pad(t.getHours())}:${pad(t.getMinutes())}:${pad(t.getSeconds())}`

Expand Down
15 changes: 15 additions & 0 deletions examples/with-redux/components/Page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Link from 'next/link'
import { connect } from 'react-redux'
import Clock from './Clock'

export default connect(state => state)(({ title, linkTo, lastUpdate, light }) => {
return (
<div>
<h1>{title}</h1>
<Clock lastUpdate={lastUpdate} light={light} />
<nav>
<Link href={linkTo}>Navigate</Link>
</nav>
</div>
)
})
2 changes: 1 addition & 1 deletion examples/with-redux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "next start"
},
"dependencies": {
"next": "*",
"next": "^2.0.0-beta",
"react-redux": "^5.0.1",
"redux": "^3.6.0",
"redux-thunk": "^2.1.0"
Expand Down
6 changes: 3 additions & 3 deletions examples/with-redux/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
import { Provider } from 'react-redux'
import { reducer, initStore, startClock } from '../store'
import Clock from '../components/Clock'
import Page from '../components/Page'

export default class Counter extends React.Component {
static getInitialProps ({ req }) {
const isServer = !!req
const store = initStore(reducer, null, isServer)
store.dispatch({ type: 'TICK', ts: Date.now() })
store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() })
return { initialState: store.getState(), isServer }
}

Expand All @@ -27,7 +27,7 @@ export default class Counter extends React.Component {
render () {
return (
<Provider store={this.store}>
<Clock />
<Page title='Index Page' linkTo='/other' />
</Provider>
)
}
Expand Down
34 changes: 34 additions & 0 deletions examples/with-redux/pages/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
import { Provider } from 'react-redux'
import { reducer, initStore, startClock } from '../store'
import Page from '../components/Page'

export default class Counter extends React.Component {
static getInitialProps ({ req }) {
const isServer = !!req
const store = initStore(reducer, null, isServer)
store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() })
return { initialState: store.getState(), isServer }
}

constructor (props) {
super(props)
this.store = initStore(reducer, props.initialState, props.isServer)
}

componentDidMount () {
this.timer = this.store.dispatch(startClock())
}

componentWillUnmount () {
clearInterval(this.timer)
}

render () {
return (
<Provider store={this.store}>
<Page title='Other Page' linkTo='/' />
</Provider>
)
}
}
2 changes: 1 addition & 1 deletion examples/with-redux/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const reducer = (state = { lastUpdate: 0, light: false }, action) => {
}

export const startClock = () => dispatch => {
setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
}

export const initStore = (reducer, initialState, isServer) => {
Expand Down