mianmalife / notebook

记录一下
2 stars 0 forks source link

redux #4

Open mianmalife opened 5 years ago

mianmalife commented 5 years ago

创建store

  1. import { createStore } from 'react'; import reducer from './reducer';

  2. const store = createStore(reducer);

3.export default store;

mianmalife commented 5 years ago

reducer

1. const defaultState = {
     inputValue: '123',
     List: [1,2]
}
2.export default (state = defaultState, action) => {
      return state;
}
mianmalife commented 5 years ago
import store './store';
store.getState();
mianmalife commented 5 years ago
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;
  }
}
mianmalife commented 5 years ago
store.subscribe(this.handle);
this.setState(store.getState());
mianmalife commented 5 years ago
actionTypes
actionCreators
mianmalife commented 5 years ago

store 是唯一的 store拿到reducer返回的数据 自己改变自己 reducer必须是纯函数

mianmalife commented 5 years ago

qq 20181031181621