/**
* 子组件如何更改父组件的state呢?
* 父组件传递下来的props不满足要求,往往需要修改
*
*
* Author: shujun
* Date: 2020-10-25
*/ import React from 'react'; export default class Father extends React.Component{
state = {
inputValue: 'shujun',
objValue: {'name': 'jay', 'sex': 'boy'}
}; changeInputValue = (e)=> {
this.setState({inputValue: e.target.value});
} changeObjValue = (e)=> {
let objValue = this.state.objValue;
objValue.name = e.target.value;
this.setState({objValue});
} render(){
const {inputValue, objValue} = this.state;
return <div style={{width: '600px', paddingBottom: '20px', border: '1px solid red' }}>
<h3>father:</h3>
<p>
react的state是可以修改,props是不让修改的,为什么要这么做呢,还不理解 ,,ԾㅂԾ,, <br/>
但是我现在就是想要修改props, 因为这样的场景很多:父组件传递下来的props不满足要求,往往需要修改
</p>
<input value={inputValue} onChange={this.changeInputValue}/>
state: inputValue -- {inputValue} <br/> <input value={objValue.name} onChange={this.changeObjValue}/>
state: objValue -- {JSON.stringify(objValue)} <br/> <Son1 inputValue={inputValue} objValue={objValue} /> <Son2 inputValue={inputValue} objValue={objValue}
changeInputValue={this.changeInputValue} changeObjValue={this.changeObjValue} />
</div>
} } class Son1 extends React.Component {
constructor(props){
super();
this.state = {flag: true};
} changeInputProps = (e)=> {
this.props.inputValue = e.target.value;
} changeObjProps = (e)=> {
console.log(e.target.value);
let objValue = this.props.objValue;
objValue.name = e.target.value;
console.log(objValue);
this.setState({flag: true});
} render() {
const {inputValue, objValue} = this.props; return <div style={{border: '1px solid green', marginTop: '20px'}}>
<h3>Son1: </h3>
<p>
只要props是对象,不改对象指针,只修改对象里面的内容, 照样能修改props, O(∩_∩)O
<br/> 但是注意:在change props后,一定要假装在setState, 引发render
</p> <input value={inputValue} onChange={this.changeInputProps}/>
props: inputValue -- {inputValue} <br/> <input value={objValue.name} onChange={this.changeObjProps}/>
props: objValue -- {JSON.stringify(objValue)} <br/>
</div>;
}
} class Son2 extends React.Component {
changeObjProps = (e)=> {
this.props.changeObjValue(e);
} render() {
const {inputValue, objValue} = this.props; return <div style={{border: '1px solid green', marginTop: '20px'}}>
<h3>Son2: </h3>
<p>
1. 父组件传递修改方法下来,子组件中调用父组件方法,修改的实际是父组件的state <br/>
2. 父组件state修改了,
</p> <input value={inputValue} onChange={(e)=>this.props.changeInputValue(e)}/>
props: inputValue -- {inputValue} <br/> <input value={objValue.name} onChange={this.changeObjProps}/>
props: objValue -- {JSON.stringify(objValue)} <br/>
</div>;
}
}

运行效果:

完整代码:https://gitee.com/loveCode666/study_react/blob/master/src/react_grammar/special_topics/1changeProps.js

最新文章

  1. BLE教程 - 官方tutorial翻译
  2. linux sudo apt-get用法详解
  3. 转: DH密钥交换和ECDH原理
  4. android开发修改相机扫描二维码框的高宽
  5. cmake,gtest单元测试程序
  6. 左移运算符&lt;&lt;
  7. SQL Server 跨库复制表方法小笔记
  8. ASP.NET MVC最新特性
  9. 阿里 java学习之路
  10. 学习札记 ----wind7下如何安装SqlServer数据库
  11. Linux系统内存占用90%以上——解决方法
  12. Spark UI界面原理
  13. 早期Swift中Cocos2D初始化代码的重构
  14. bak
  15. Contest Hunter 1401 兔子与兔子
  16. matlab画直线,指定斜率与x坐标范围
  17. iOS UITextField更改placeholder颜色
  18. Spring框架第三篇之基于XML的DI注入
  19. redis 面试题1 有用
  20. 【IntelliJ IDEA】在idea上操作 git分支合并【如何将远程swagger分支 合并到 远程 master分支上】【如何切换 本地分支】

热门文章

  1. hive中对时间处理的函数总结
  2. 沁恒蓝牙系列芯片USB烧录故障排查
  3. centos 7 删除乱码文件
  4. 搭建 springboot 应用
  5. vue3 门户网站搭建4-mockjs
  6. 【当年笔记】Collection集合部分
  7. DBeaver导入SQL脚本数据
  8. sql语句查询优化
  9. python之目录结构01
  10. 搭建Kubord管理k8s/EKS以及Harbor私有仓库教程