实现
read.js
绘制DOM以及链接 couter 的 pr函数
import { add, minus, asyncAdd } from '../../actions/counter'import './index.scss'@connect(({ pr }) => ({pr}), (dispatch) => ({add () {dispatch(add())},dec () {dispatch(minus())},asyncAdd () {dispatch(asyncAdd())}}))class Index extends Component {config = {navigationBarTitleText: '阅览中心'}componentWillReceiveProps (nextProps) {console.log(this.props, nextProps)}render () {return (<View className='index'><View className='at-row'><View className='at-col' ><Button className='add_btn' onClick={this.props.add}>座位数{this.props.pr.num}</Button></View></View></View>)}}export default Index
reducers/counter.js
在reducers里面描述actions 和 store 的state关系 ,定义一个 pr 的函数,数量的初始值为0,点击一次+1
只有actions 可以改变state tree 的状态,reducers是具体怎么改变,actions大概是需要改什么(类似大纲)
const INITIAL_STATE = {num: 0}export default function pr (state = INITIAL_STATE, action) {switch (action.type) {case ADD:return {...state,num: state.num + 1}case MINUS:return {...state,num: state.num - 1}default:return state}}
reducers/index.js
导出 reducers的 pr 函数
export default combineReducers({pr})
细节注意:函数名
小程序初始状态下 pr会被定义为counter,由于 里面有多个 counter.js 文件,容易引起歧义而使得逻辑比较混乱,建议起一个其他的名字。
