react - 组件间的值传递
父组件向子组件传值
- 父组件通过属性进行传递,子组件通过props获取
//父组件
class CommentList extends Component{
render(){
return(
<div>
<Comment comment={information}/>
</div>
)
}
}
//子组件
class Comment extends Component{
render(){
return(
<div>
<span>{this.props.comment}:</span>
</div>
)
}
}
父组件ComentList引用子组件Comment时设置comment属性传递information,在在组件Comment中通过this.props.comment获取到information值
子组件向父组件传值
- 通过回调进行传递数据
//父组件
class CommentApp extends Component{
constructor(){
super();
this.state = {comments:[]}
}
printContent(comment){
this.setState({comment});
}
render(){
return (
<div>
<CommentInput onSubmit={this.printContent.bind(this)} />
</div>
)
}
}
//子组件
class CommentInput extends Component{
constructor(){
super();
this.state = {
usrName:'',
content:''
};
}
submit(){
if(this.props.onSubmit){
var {usrName,content} = this.state;
this.props.onSubmit({usrName,content})
}
this.setState({content:''});
}
render(){
return(
<div>
<div>
<span>用户名:</span>
<div className="usrName">
<input type="text" value={this.state.usrName} />
</div>
</div>
<div>
<span>评论内容:</span>
<div className="content">
<textarea value={this.state.content} />
</div>
</div>
<button onClick={this.submit.bind(this)}>submit</button>
</div>
)
}
}
在父组件CommentApp中调用CommentInput通过属性onSubmit传入函数printContent,在子组件CommentInput中通过this.props.onsubmit调用函数通过参数形式进行值的传递
兄弟组件之间的传值
- 通过相同的父组件进行传递数据
子组件A通过回调函数形式将数据传给父组件,接着父组件通过属性将数据传递给子组件B
- 通过发布/订阅进行传递
在子组件A中 commentDidMount函数中,发布事件,在子组件B中commentDidMount函数中对事件进行监听
- 使用context进行传递
class Parent extends Component(
constructor(props) {
super(props);
this.state = { value: '' }
}
getChildContext(){
return {
this.value = this.state.value;
}
} render() {
return (
<div>
<Child1 />
<Child2 />
</div>
)
}
} class Child1 extends Component{
change:function(){
this.context.value = "heiheihei"
}
render() {
return (
<div>
子组件一
<p>{this.context.value}</p>
</div>
)
}
} class Child2 extends Component{
render() {
return (
<div>
子组件二
<p>{this.context.value}</p>
</div>
)
}
}
context局限性
1. context在react中只是实验阶段未来可能改变
2. 若shouldComponentUpdate中return false会影响context的传值,使子组件无法更新
3. 组件发purComponent也会影响context的传值,影响子组件的更新
- 使用redux对state进行统一管理
react - 组件间的值传递的更多相关文章
- React 组件间通讯
React 组件间通讯 说 React 组件间通讯之前,我们先来讨论一下 React 组件究竟有多少种层级间的关系.假设我们开发的项目是一个纯 React 的项目,那我们项目应该有如下类似的关系: 父 ...
- vue-cli中父子组件间的变量传递
vue-cli中父子组件间的变量传递 在vue中每一个组件的作用域都是独立的,如果我们想实现父子组件间变量的传递就要另寻他法,而不能直接调用其中的变量. 父级组件向子级组件传递变量 要实现这种效果我们 ...
- React 组件间通信介绍
React 组件间通信方式简介 React 组件间通信主要分为以下四种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面对这四种情况分别进行介绍: 父组件向子 ...
- vue 和 react 组件间通信方法对比
vue 和 react 组件间通信方法对比: 通信路径 vue的方法 react的方法 父组件 => 子组件 props(推荐).slot(推荐).this.$refs.this.$childr ...
- vue.js 同级组件之间的值传递方法(uni-app通用)
vue.js 兄弟组件之间的值传递方法 https://blog.csdn.net/jingtian678/article/details/81634149
- react组件间的传值方法
关于react的几个网站: http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.re ...
- React组件间信息传递方式
组件之间传递信息方式,总体可分为以下5种: 1.(父组件)向(子组件)传递信息 2.(父组件)向更深层的(子组件) 进行传递信息 >>利用(context) 3.(子组件)向(父组件)传 ...
- React组件间的通讯
组件化开发应该是React核心功能之一,组件之间的通讯也是我们做React开发必要掌握的技能.接下来我们将从组件之间的关系来分解组件间如何传递数据. 1.父组件向子组件传递数据 通讯是单向的,数据必须 ...
- vue组件间的数据传递
父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据. App.vue HelloWorld.vue 在子组件部分: 如果需要从父组件获取 logo 的值,就需要使用 p ...
随机推荐
- ppt课件动手动脑实际验证
1关于double精度 源代码:public class Doublejingdu { public static void main(String[] args) { System.out.prin ...
- Java面向对象笔记(五分钟秒懂)
面向对象概念 面向对象三大特征:封装,继承,多态 面向对象编程(OOP,Object Oriented Programing)是相对于面向过程编程说的,之前写的代码基本都是纯的面向过程编程的,当项目复 ...
- eShopOnContainers学习系列(二):数据库连接健康检查
项目里使用数据库的时候,我们有时候需要知道数据库当前的健康状态,特别是当数据库连接不上的时候能够立马获悉.eShopOnContainers里存在着大量的服务健康.连接健康的检查,数据库连接是其中之一 ...
- JAVA TCP/IP网络通讯编程(一)
一个实例通过client端和server端通讯 客户端发送:“我是客户端,请多关照” 服务端回复:“收到来自于"+s.getInetAddress().getHostName()+" ...
- Nature Methods | 新软件SAVER-X可对单细胞转录组学数据进行有效降噪
图片来源(Nature Methods) 摘要 单细胞转 ...
- Network in Network(2013),1x1卷积与Global Average Pooling
目录 写在前面 mlpconv layer实现 Global Average Pooling 网络结构 参考 博客:blog.shinelee.me | 博客园 | CSDN 写在前面 <Net ...
- 如何在 Linux 和 windows 启动使用 Dubbo 和 ZK
今天的知识点有 3 个: Linux 下安装.配置 JDK. Windows 下启启动 dubbo 和 zookeeper. Linux 下启动 dubbo 和 zookeeper. 1.Linux ...
- MAC 上的Phantomjs的安装和配置
1.下载 http://phantomjs.org/download.html 选择mac版本下载 2.下载完成后,解压缩,然后放到自己的一个目录下面 例如:/usr/local/Phantomjs/ ...
- nm 命令能够显示目标文件中重载函数的名字改变(C++)
#include <stdio.h> #include <iostream> using std::cout; using std::endl; //这里的两个不同的add函数 ...
- python常用算法(5)——树,二叉树与AVL树
1,树 树是一种非常重要的非线性数据结构,直观的看,它是数据元素(在树中称为节点)按分支关系组织起来的结构,很像自然界中树那样.树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构都可用树形 ...