react-redux 基本用法
import {createStore} from 'redux';
//渲染函数,初始化state和改变state的状态action
function counter(state = 0, action) {
switch (action.type) {
case 'add':
return state + 1;
case 'sub':
return state - 1;
default:
return 10;
}
}
//1.新建store
const store = createStore(counter);
const init = store.getState();//获取store的状态
console.log(init);
//观察者模式;每次dispatch时就会执行这个操作;
function listener() {
const current = store.getState();
console.log(current)
}
store.subscribe(listener);
//派发事件
store.dispatch({type: 'add'});
store.dispatch({type: 'add'});