Open mianmalife opened 5 years ago
reducer
1. const defaultState = {
inputValue: '123',
List: [1,2]
}
2.export default (state = defaultState, action) => {
return state;
}
import store './store';
store.getState();
handle = (e) => {
const action = {
type: 'add_item',
inputValue: e.target.value,
}
store.dispatch(action);
}
export default (state = defaultState, action) => {
if(action.type==='add_item') {
newState = JSON.parse(JSON.stringfy(state));
newState.List.push(action.inputValue);
return newState;
}
}
store.subscribe(this.handle);
this.setState(store.getState());
actionTypes
actionCreators
store 是唯一的 store拿到reducer返回的数据 自己改变自己 reducer必须是纯函数
创建store
import { createStore } from 'react'; import reducer from './reducer'
;const store = createStore(reducer)
;3.
export default store
;