react--记录(store得创建)

建立一个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


版权声明:本文为qq_39516708原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。