javascript 下一步和Redux示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 下一步和Redux示例相关的知识,希望对你有一定的参考价值。
import App, { Container } from 'next/app'
import React from 'react'
import withReduxStore from '../lib/with-redux-store'
import { Provider } from 'react-redux'
class MyApp extends App {
render () {
const { Component, pageProps, reduxStore } = this.props
return (
<Container>
<Provider store={reduxStore}>
<Component {...pageProps} />
</Provider>
</Container>
)
}
}
export default withReduxStore(MyApp)
import React from 'react'
import { connect } from 'react-redux'
import { startClock, serverRenderClock } from '../store'
import Examples from '../components/examples'
class Index extends React.Component {
static getInitialProps ({ reduxStore, req }) {
const isServer = !!req
reduxStore.dispatch(serverRenderClock(isServer))
return {}
}
componentDidMount () {
const { dispatch } = this.props
this.timer = startClock(dispatch)
}
componentWillUnmount () {
clearInterval(this.timer)
}
render () {
return <Examples />
}
}
export default connect()(Index)
import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'
const exampleInitialState = {
lastUpdate: 0,
light: false,
count: 0
}
export const actionTypes = {
TICK: 'TICK',
INCREMENT: 'INCREMENT',
DECREMENT: 'DECREMENT',
RESET: 'RESET'
}
// REDUCERS
export const reducer = (state = exampleInitialState, action) => {
switch (action.type) {
case actionTypes.TICK:
return Object.assign({}, state, {
lastUpdate: action.ts,
light: !!action.light
})
case actionTypes.INCREMENT:
return Object.assign({}, state, {
count: state.count + 1
})
case actionTypes.DECREMENT:
return Object.assign({}, state, {
count: state.count - 1
})
case actionTypes.RESET:
return Object.assign({}, state, {
count: exampleInitialState.count
})
default:
return state
}
}
// ACTIONS
export const serverRenderClock = isServer => dispatch => {
return dispatch({ type: actionTypes.TICK, light: !isServer, ts: Date.now() })
}
export const startClock = dispatch => {
return setInterval(() => {
// Dispatch `TICK` every 1 second
dispatch({ type: actionTypes.TICK, light: true, ts: Date.now() })
}, 1000)
}
export const incrementCount = () => dispatch => {
return dispatch({ type: actionTypes.INCREMENT })
}
export const decrementCount = () => dispatch => {
return dispatch({ type: actionTypes.DECREMENT })
}
export const resetCount = () => dispatch => {
return dispatch({ type: actionTypes.RESET })
}
export function initializeStore (initialState = exampleInitialState) {
return createStore(
reducer,
initialState,
composeWithDevTools(applyMiddleware(thunkMiddleware))
)
}
import { connect } from 'react-redux'
import Clock from './clock'
import Counter from './counter'
function Examples ({ lastUpdate, light }) {
return (
<div>
<Clock lastUpdate={lastUpdate} light={light} />
<Counter />
</div>
)
}
function mapStateToProps (state) {
const { lastUpdate, light } = state
return { lastUpdate, light }
}
export default connect(mapStateToProps)(Examples)
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { incrementCount, decrementCount, resetCount } from '../store'
class Counter extends Component {
increment = () => {
const { dispatch } = this.props
dispatch(incrementCount())
}
decrement = () => {
const { dispatch } = this.props
dispatch(decrementCount())
}
reset = () => {
const { dispatch } = this.props
dispatch(resetCount())
}
render () {
const { count } = this.props
return (
<div>
<h1>
Count: <span>{count}</span>
</h1>
<button onClick={this.increment}>+1</button>
<button onClick={this.decrement}>-1</button>
<button onClick={this.reset}>Reset</button>
</div>
)
}
}
function mapStateToProps (state) {
const { count } = state
return { count }
}
export default connect(mapStateToProps)(Counter)
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;
}
.light {
background-color: #999;
}
`}</style>
</div>
)
}
const format = t =>
`${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}`
const pad = n => (n < 10 ? `0${n}` : n)
以上是关于javascript 下一步和Redux示例的主要内容,如果未能解决你的问题,请参考以下文章