/**
* 子组件如何更改父组件的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

React如何修改props && 子组件调用父组件方法的更多相关文章

  1. React篇-子组件调用父组件方法,并传值

    react 中子组件调用父组件的方法,通过props: 父组件: isNote(data){} <div className="tabC01"> <FTab ta ...

  2. react typescript 子组件调用父组件

    //父组件 import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &q ...

  3. react 子组件调用父组件方法

    import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...

  4. vue 子组件调用父组件的方法

    vue中 父子组件的通信: 子组件通过 props: { //子组件中写的. childMsg: { //字段名 type: Array,//类型 default: [0,0,0] //这样可以指定默 ...

  5. Vue子组件调用父组件的方法

    Vue子组件调用父组件的方法   Vue中子组件调用父组件的方法,这里有三种方法提供参考 第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法 父组件 <temp ...

  6. Vue 子组件调用父组件 $emit

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  7. vue 子组件调用父组件的函数

    子组件调用父组件的函数,使用$emit(eventName,[...args]),触发当前实例上的事件.附加参数都会传给监听器回调. 子组件 <template> <div> ...

  8. Vue中子组件调用父组件的方法

    Vue中子组件调用父组件的方法 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. react 父组件调用子组件方法、子组件调用父组件方法

    我们闲话不多说,直接上代码 // 父组件 import React, {Component} from 'react'; class Parents extends Component { const ...

  10. Flutter子组件调用父组件方法修改父组件参数

    子组件调用父级组件方法的主要实现是父组件给子组件传入一个方法,然后在子组件中调用父级方法来修改父级的参数.看一下效果图 父级组件实现 在父级组件中写一个_editParentText的方法来修改组件中 ...

随机推荐

  1. spring-boot logback 日志

    一.引入依赖 二.配置日志文件 三.完事啦!

  2. JS 时间的获取和比较

    JS获取时间 获取当前时间 var date = new Date(); 可指定某种格式来获取时间,或者将字符串转换成时间 var date = new Date("2019-09-24 T ...

  3. 音乐下载器,音乐解析软件,全网音乐免费下载,mp3格式音乐下载,flac格式音乐下载,无损音质音乐下载器,你想听的都搜的到~

    在这个音乐版权被三分天下的时代,想必大家也都会有这种的困扰,喜欢的音乐很多,刚好这些音乐的版权还分散在三大主流音乐厂商的手里. 这样的话,想要听或者下载自己喜欢的音乐可能要开多个会员,而且下载的音乐单 ...

  4. C# 日期获得一天的起始时间

    dateTimePicker1.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")); ...

  5. JDK下载很慢

    方法1.直接百度jdk镜像,搜到华为镜像:https://repo.huaweicloud.com/java/jdk/ 方法2.到官网拷贝下载的url:https://download.oracle. ...

  6. 【QT+MSVC2015】不安装VS2015,QT配置MSVC2015编译环境

    本文介绍不安装VS2015的情况下在QT5.10中配置MSVC2015编译器. 系统:windows系统 QT版本:5.10.1 所需文件: QT安装程序--qt-opensource-windows ...

  7. [CSAPP]第一章 计算机系统漫游 学习笔记

    CSAPP 第一章 计算机系统漫游 1.1 信息就是位+上下文 系统中所有信息-----包括磁盘文件.内存中的程序.内存中存放的用户数据以及网络上上传的数据,都是由一串比特表示的.同时区分不同数据对象 ...

  8. Firefox彻底禁用自动更新提示

    实验版本:96 安装目录下新建文件夹:distribution 新建文件:policies.json 文件内容为: { "policies": { "DisableApp ...

  9. VirtualBox + Parrot

    安装环境:win8 1.官网下载virtualbox安装包,parrot kde sequrity镜像文件 2.用virtualbox创建新的虚拟机,内存4g,类型是linux debian 64,硬 ...

  10. python 在路径下创建文件/文本文件 没有路径自动创建

    1.一般在执行文件的同级目录下创建一个文本文件: file = open("1.txt", "w", encoding="utf8") # ...