zoukankan      html  css  js  c++  java
  • react redux

        

    App.js
    import React, { Component } from 'react'; import store from './redux'; import Com from "./com" class App extends Component { constructor(props){ super(props); console.log(store); console.log(store.getState());//获取数据 this.save = this.save.bind(this); this.state = { message: "", inputValue: "" }; store.subscribe(this.handleStoreChange);//监听store中的state改变 } render() { return ( <div className="App"> <input value = {this.inputValue} onChange={this.inputChange} /> <button onClick={this.save}>存储state</button> <p>{this.state.message}</p> <Com /> </div> ); } save(){ const action = { type: "msg",//必须有 value: this.state.inputValue } store.dispatch(action);//改变redux/reducer.js的state } handleStoreChange = () => { this.setState((preState) => { const newState = JSON.parse(JSON.stringify(preState)); const storeData = store.getState(); console.log(storeData) newState.message = storeData.message; return newState; }); } inputChange = (e) => { const value = e.target.value; this.setState((preState) => { const newState = JSON.parse(JSON.stringify(preState)); newState.inputValue = value; return newState; }); } } export default App;
    redux/index.js
    import { createStore } from 'redux';
    import reducer from './reducer.js';
    
    export default createStore(reducer); 
    
    redux/reducer.js
    export default (state = {}, action) => {
    	console.log(state,action);
    	const newState = JSON.parse(JSON.stringify(state));
    	if(action.type == "msg"){
    		newState.message = action.value;
    		return newState;
    	}
    	
    	if(action.type == "msg-msg"){
    		newState.message = action.value;
    		return newState;
    	}
    	
    	return newState;
    }
    

      

      

      

  • 相关阅读:
    在CentOS7上搭建本地yum仓库
    CentOS 7 64位虚拟机安装过程
    汇编:1位16进制数到ASCII码转换
    汇编:3个数排序(从大到小)
    汇编:滤去(删除)某个字符串中空格符号
    汇编:计算字符串长度
    汇编:字符串小写变大写(子程序)
    汇编:输出寄存器AX中的内容(子程序)
    汇编:输出寄存器AX中的内容
    使用汇编语言将输入的字符串大写转换为小写
  • 原文地址:https://www.cnblogs.com/whlBooK/p/10838096.html
Copyright © 2011-2022 走看看