父组件向子组件传值

  • 父组件通过属性进行传递,子组件通过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 - 组件间的值传递的更多相关文章

  1. React 组件间通讯

    React 组件间通讯 说 React 组件间通讯之前,我们先来讨论一下 React 组件究竟有多少种层级间的关系.假设我们开发的项目是一个纯 React 的项目,那我们项目应该有如下类似的关系: 父 ...

  2. vue-cli中父子组件间的变量传递

    vue-cli中父子组件间的变量传递 在vue中每一个组件的作用域都是独立的,如果我们想实现父子组件间变量的传递就要另寻他法,而不能直接调用其中的变量. 父级组件向子级组件传递变量 要实现这种效果我们 ...

  3. React 组件间通信介绍

    React 组件间通信方式简介 React 组件间通信主要分为以下四种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面对这四种情况分别进行介绍:   父组件向子 ...

  4. vue 和 react 组件间通信方法对比

    vue 和 react 组件间通信方法对比: 通信路径 vue的方法 react的方法 父组件 => 子组件 props(推荐).slot(推荐).this.$refs.this.$childr ...

  5. vue.js 同级组件之间的值传递方法(uni-app通用)

    vue.js 兄弟组件之间的值传递方法 https://blog.csdn.net/jingtian678/article/details/81634149

  6. react组件间的传值方法

    关于react的几个网站: http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.re ...

  7. React组件间信息传递方式

    组件之间传递信息方式,总体可分为以下5种: 1.(父组件)向(子组件)传递信息 2.(父组件)向更深层的(子组件) 进行传递信息  >>利用(context) 3.(子组件)向(父组件)传 ...

  8. React组件间的通讯

    组件化开发应该是React核心功能之一,组件之间的通讯也是我们做React开发必要掌握的技能.接下来我们将从组件之间的关系来分解组件间如何传递数据. 1.父组件向子组件传递数据 通讯是单向的,数据必须 ...

  9. vue组件间的数据传递

    父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据.   App.vue HelloWorld.vue 在子组件部分: 如果需要从父组件获取 logo 的值,就需要使用 p ...

随机推荐

  1. DOM之节点操作

    DOM提供了很多实用的API,这些API让我们可以轻松的访问HTML文档.所谓API(应用程序接口),简单来说,就是让我们可以直接使用它访问程序的一些属性或方法,而不用了解程序内部的运作过程和原理. ...

  2. 2018年蓝桥杯java b组第五题

    标题:快速排序 以下代码可以从数组a[]中找出第k小的元素. 它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的. 请仔细阅读分析源码,填写划线部分缺失的内容. 我在使用(a, l, r, ...

  3. 品Spring:SpringBoot轻松取胜bean定义注册的“第一阶段”

    上一篇文章强调了bean定义注册占Spring应用的半壁江山.而且详细介绍了两个重量级的注册bean定义的类. 今天就以SpringBoot为例,来看看整个SpringBoot应用的bean定义是如何 ...

  4. swagger2的简单使用

    swagger2的简单使用 优点: 可以生成文档形式的API并提供给不同的团队使用 便于自己单测 无需过多冗余的word文档,这一点很重要,因为我在工作中就遇到这么一个情况,由于开发使用的文档和最新文 ...

  5. WPF 将字体文件 添加到 资源文件,并在后台代码使用

    先看结果 1.将字体文件,导入到资源文件,如: 添加后,自动生成 2.在窗体xaml中添加如: 3.在xaml窗体对应的cs文件中,为TextBlock指定字段 创建一个字段对象,并指定字体文件的所在 ...

  6. WebGL简易教程(五):图形变换(模型、视图、投影变换)

    [toc] 1. 概述 通过之前的教程,对WebGL中可编程渲染管线的流程有了一定的认识.但是只有前面的知识还不足以绘制真正的三维场景,可以发现之前我们绘制的点.三角形的坐标都是[-1,1]之间,Z值 ...

  7. mysql root用户登录后无法查看数据库全部表

    可能是把root@localhost用户删掉了. 首先停掉mysql服务,在/etc/my.cnf中添加 skip-grant-tables,同时可以添加skip-networking选项来禁用网络功 ...

  8. [LeetCode] 822. Card Flipping Game

    Description On a table are N cards, with a positive integer printed on the front and back of each ca ...

  9. SoapUI 的几种常用参数化方式

    今天给大家来梳理下soapui这款工具关于参数化的几种方式以及具体的应用场景 1.properties 官方文档:https://www.soapui.org/docs/functional-test ...

  10. 设计一个A表数据抽取到B表的抽取过程

    原题如下: 解题代码如下: table1类: @Data @NoArgsConstructor @AllArgsConstructor public class table1{ private Str ...