最简单的一个react-redux 示例,实现点击加一的计数器
import React from 'react'
import ReactDOM from 'react-dom'
import {createStore} from 'redux'
import {Provider,connect} from 'react-redux'
function reducers(state = 1,action) {
switch (action.type) {
case 'ADD':
return state + action.payload;
default:
return state;
}
};
const store = createStore(reducers)
const ResultIn = (props) => <div>结果:{props.value}</div>
const Result = connect(
mapStateToPropsResult
)(ResultIn)
var AppIn = (props) => <div onClick={() => {
props.onChange()
}}>点我加1<Result/></div>
function mapStateToPropsResult(state) {
return {
value: state
}
}
function mapStateToProps(state) {
return {
value: state
}
}
function mapDispatchToProps(dispatch) {
return {
onChange: () => dispatch({type: 'ADD',payload: 1})
}
}
const App = connect(
mapStateToProps,mapDispatchToProps
)(AppIn)
ReactDOM.render(
<Provider store={store}>
<App/>
</Provider>,document.getElementById('root')
)