建立一个store 文件夹
建立两个文件【index.js】+【reducer】
index:入口文件
import { createStore } from 'redux';
import reducer from './reducer';
const store = createStore(reducer);
export default store;
reducer:数据库文件
const defaultState = {
list:[],
inputValue: ''
};
export default (state = defaultState,action) => {
if(action.type === 'change_inputValue'){
const newState = JSON.parse(JSON.stringify(state)) //深拷贝
newState.inputValue = action.inputValue;
return newState
}
return state;
}
更新state数据重新渲染函数:
store.subscribe(this.chan_state) //直接在state里面调用
chan_state(){
this.setState(store.getState)
}
让页面数据等于store数据
constructor(props){
super(props)
this.state = store.getState()
}
把action传给reducer
default.a.getState is not a function