React如何修改props && 子组件调用父组件方法
/**
* 子组件如何更改父组件的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>;
}
}
运行效果:

React如何修改props && 子组件调用父组件方法的更多相关文章
- React篇-子组件调用父组件方法,并传值
react 中子组件调用父组件的方法,通过props: 父组件: isNote(data){} <div className="tabC01"> <FTab ta ...
- react typescript 子组件调用父组件
//父组件 import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &q ...
- react 子组件调用父组件方法
import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...
- vue 子组件调用父组件的方法
vue中 父子组件的通信: 子组件通过 props: { //子组件中写的. childMsg: { //字段名 type: Array,//类型 default: [0,0,0] //这样可以指定默 ...
- Vue子组件调用父组件的方法
Vue子组件调用父组件的方法 Vue中子组件调用父组件的方法,这里有三种方法提供参考 第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法 父组件 <temp ...
- Vue 子组件调用父组件 $emit
<!DOCTYPE html><html> <head> <meta charset="utf-8"> ...
- vue 子组件调用父组件的函数
子组件调用父组件的函数,使用$emit(eventName,[...args]),触发当前实例上的事件.附加参数都会传给监听器回调. 子组件 <template> <div> ...
- Vue中子组件调用父组件的方法
Vue中子组件调用父组件的方法 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- react 父组件调用子组件方法、子组件调用父组件方法
我们闲话不多说,直接上代码 // 父组件 import React, {Component} from 'react'; class Parents extends Component { const ...
- Flutter子组件调用父组件方法修改父组件参数
子组件调用父级组件方法的主要实现是父组件给子组件传入一个方法,然后在子组件中调用父级方法来修改父级的参数.看一下效果图 父级组件实现 在父级组件中写一个_editParentText的方法来修改组件中 ...
随机推荐
- Django 之 ModelForm
一 Django的form组件 forms组件 二 Django的model form组件 这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,先来一个简单的 ...
- taro 学习笔记
1.Taro 是一个开放式跨端跨框架解决方案,支持使用 React/Vue/Nerv 等框架来开发 微信 / 京东 / 百度 / 支付宝 / 字节跳动 / QQ / 飞书 小程序 / H5 / RN ...
- 【Linux】Linux网络编程
socket 默认开启的socket是阻塞的,想要设置非阻塞的话,可以用 fcntl设置为 nonblock:或者使用setsockopt进行更多设置: 一般使用推荐 设成nonblock,然后epo ...
- Linux网络第四章:SSH远程管理及通过SSH实现服务器之间的免密连接
目录 一.SSH远程管理基础 1.ssh协议 2.ssh原理 3.配置文件解析 4.登录方法 5.使用ssh协议传输的命令 二.免密连接的实现 1.免密连接原理 2.免密实现环境准备 3.在客户端生成 ...
- springboot+Elasticsearch 复杂查询
以前没做过ES 里面的查询,第一次接触还是走了点弯路的. 就是这个字段你在ES 都不用模糊查的话,就可以设置 type = FieldType.Keyword,比如ID之类的. 一:建ES存储的实体 ...
- 思科数据中心CCIE稳定PASS
乾颐堂DC数据中心CCIE,在增加新题的情况下一次PASS! 目前笔试.lab都在过人,需要备考的同学联系乾颐堂,机时充足!
- [CSAPP]第一章 计算机系统漫游 学习笔记
CSAPP 第一章 计算机系统漫游 1.1 信息就是位+上下文 系统中所有信息-----包括磁盘文件.内存中的程序.内存中存放的用户数据以及网络上上传的数据,都是由一串比特表示的.同时区分不同数据对象 ...
- Java程序(数组扩容的尝试)
import java.util.Scanner; public class ArrayAdd { public static void main(String[] args) { int arr[] ...
- Python_基础_Print_转义字符和原字符
转义字符和原字符 print('hello\nworld') #转义功能的首字母 n-->newline的首字母表示换行 print('hello\tworld') #\t即表示一个制表位 pr ...
- 前端之Vue day07 混入、插件、elementui、Router、Vuex
一.Props补充 1.父传子在子组件标签上起自定义属性 使用数组 就不演示了,太简单了 2.限制传入的数据类类型 使用对象 同样,展示过的 3.props补充 就是套对象,加以限制 props:{ ...