首先,得安装react-redux(npm install --save react-redux
)。
本篇将涉及的文件有:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render( <App />,document.getElementById("root"));
import React, { Component } from 'react'
import CounterContainer from "./containers/CounterContainer";
import store from "./redux/store.js";
export default class App extends Component {
render() {
return (
<div>
<CounterContainer store={store}/>
</div>
)
}
}
export const INCREMENT = 'increment';
export const DECREMENT = 'decrement';
import { INCREMENT,DECREMENT } from "./constant"
export const createIncrementAction = data => ({type:INCREMENT,data});
export const createDecrementAction = data => ({type:DECREMENT,data});
export const createIncrementAsyncAction = (data,time) => {
return (dispatch) => {
setTimeout(() => {
dispatch(createIncrementAction(data));
},time)
}
}
import { INCREMENT,DECREMENT } from "./constant";
const initState = 0
export default function reducer(preState=initState,action){
const {type,data} = action;
switch(type){
case INCREMENT: return preState+Number(data);
case DECREMENT: return preState-Number(data);
default: return preState
}
}
import { createStore,applyMiddleware} from "redux";
import thunk from "redux-thunk";
import counterReducer from "./reducer.js";
export default createStore(counterReducer,applyMiddleware(thunk));
import Counter from "../../components/Counter";
import {createIncrementAction,createDecrementAction,createIncrementAsyncAction} from "../../redux/action.js";
import { connect } from "react-redux";
const mapStateToProps = state => ({count:state})
const mapDispatchToProps = dispatch => ({
jia: data => (dispatch(createIncrementAction(data))),
jian: data => (dispatch(createDecrementAction(data))),
jiaAsync: (data,time) => (dispatch(createIncrementAsyncAction(data,time)))
})
export default connect(mapStateToProps,mapDispatchToProps)(Counter);
const mapStateToProps = (state) => ({})
const mapDispatchToProps = (dispatch) => ({})
connect(mapStateToProps,mapDispatchToProps)(UI组件)
返回的结果是 容器组件。
mapDispatchToProps
也可以是一个对象,如下。生成action后,react-redux会自动分发(dispatch)。
import Counter from "../../components/Counter";
import {createIncrementAction,createDecrementAction,createIncrementAsyncAction} from "../../redux/action.js";
import { connect } from "react-redux";
const mapStateToProps = (state) => ({count:state})
const mapDispatchToProps = {
jia:createIncrementAction,
jian:createDecrementAction,
jiaAsync:createIncrementAsyncAction
}
export default connect(mapStateToProps,mapDispatchToProps)(Counter);
import React, { Component } from 'react'
export default class Counter extends Component {
increment = () => {
const {value} = this.selectEl;
this.props.jia(Number(value));
}
decrement = () => {
const {value} = this.selectEl;
this.props.jian(Number(value));
}
incrementOdd = () => {
const {value} = this.selectEl;
if(this.props.count % 2 !== 0){
this.props.jia(Number(value));
}
}
incrementWait = () => {
const {value} = this.selectEl;
this.props.jiaAsync(Number(value),1000)
}
render() {
const {increment,decrement,incrementOdd,incrementWait} = this;
return (
<div>
<h2>当前求和为{this.props.count}</h2>
<select ref={c => this.selectEl = c}>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
<button onClick={incrementOdd}>和为奇数时加</button>
<button onClick={incrementWait}>等一等再加</button>
</div>
)
}
}
所有UI组件都被一个容器组件包裹,容器组件、UI组件是父子关系。
真正和redux打交道的是容器组件,容器组件可以使用redux的API。
UI组件不能使用redux的API。
容器组件通过props向容器组件传递:redux中保存的状态、操作状态的方法。
求和案例(纯react版本)
求和案例(redux精简版)
求和案例(redux完整版)
求和案例(redux完整版)(异步action)