redux基本使用方法
2017-12-27 本文已影响0人
小梁姐姐
import {createState} from 'redux'
export default function() {
//定义计算规则。即reducer
function counter(state= 0, action) {
switch(action.type){
case 'INCREMENT':
return state+1
case 'DECREMENT':
return state-1
default:
return state
}
}
//根据计算规则生成store
let store = createState(counter)
//定义数据(state)变化之后的派发规则
store.subscribe(() => {
console.log(store.getState())
})
//触发数据变化
store.dispatch({type: 'INCREMENT'}) //1
store.dispatch({type: 'INCREMENT'}) //2
store.dispatch({type: 'DECREMENT'}) //1
}