import Children from ‘./Children’;
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg:‘父组件传递给子组件’,
fromChildrn:‘’
};
this.changeMsg = this.changeMsg.bind(this)
}
changeMsg(val){
this.setState({
fromChildrn: val
})
}
render(){
return (
父子组件通信实例
<span style={{color:‘red’}}>{this.state.fromChildrn}
)
}
}
export default Parent
在子组件中,用this.props.callback()执行父组件的回调函数,从而执行绑定方法changeMsg,显示子组件传过来的值
import React from ‘react’;
import { Button } from ‘element-react’;
class Children extends React.Component {
constructor(props) {
super(props);
this.state = {
msg:this.props.toChildren
};
this.toParent = this.toParent.bind(this)
}
toParent(){
this.props.callback(‘子组件传过来的值’) //子组件通过此触发父组件的回调方法
}
render(){
return (
从父组件传过来:
<span style={{color:‘blue’}}>{this.state.msg}
子传父
)
}
}
export default Children
注意:props中的回调函数名称需一致,即本例中的callback,如下
小结: 以上为直接父子组件通信的其中一种方式,父传子,通过props;子传父,执行回调。
二、跨级组件通信
假设一个父组件中存在一个子组件,这个子组件中又存在一个子组件,暂且称为“孙组件”,当父组件需要与“孙组件”通信时,常用的方式有两种,逐层传值与跨层传值。
1、逐层传值
这种方式就是上面的直接父子通信的基础上在加上一个中间层。如父、“孙”组件通信,可以先父子通信,然后再子“孙”通信,传递的层级变成父–>子–>“孙”,同理,通过props往下传,通过回调往上传。不展开,有兴趣的自己动手实现一下。
2、跨级传值
顾名思义,父跟“孙”通信,不需要经过子(中间层)组件。这里引出了Context。
React官方文档对Context做出了解释:
在一个典型的 React 应用中,数据是通过 props 属性自上而下(由父及子)进行传递的,但这种做法对于某些类型的属性而言是极其繁琐的(例如:地区偏好,UI 主题),这些属性是应用程序中许多组件都需要的。Context 提供了一种在组件之间共享此类值的方式,而不必显式地通过组件树的逐层传递 props。
一句话概括就是:跨级传值,状态共享。
看下简单的实例,直接讲用法。
首先,我先创建一个context.js文件(与父子孙同个目录),默认值为一个对象。
import React from “react”;
const MyContext = React.createContext({text:‘luck’});
export default MyContext
然后,对父组件进行改写,引入context,使用一个 Provider 来将当前的 value 传递给以下的组件树,value为传递的值。
import React from ‘react’;
import Children from ‘./Children’;
import MyContext from ‘./context’;
class Parent extends React.Component {
constructor(props) {
super(props);
}
// 使用一个 Provider 来将当前的 value 传递给以下的组件树。
// 无论多深,任何组件都能读取这个值。
render(){
return (
context通信实例
<MyContext.Provider value={{text:‘good luck’}}>
</MyContext.Provider>
)
}
}
export default Parent
子组件为中间层,不做处理,用于包裹“孙”组件。
import React from ‘react’;
import Grandson from ‘./Grandson’;
class Children extends React.Component {
render(){
return (
)
}
}
export default Children
新增一个“孙”组件,同样需引入context,在组件内部添加static contextType = MyContext,此时将能通过this.context直接获取到上层距离最近的Provider传递的值,此时this.context = {text:good luck},即父组件传递value。
import React from ‘react’;
import MyContext from ‘./context’;
class Grandson extends React.Component {
static contextType = MyContext
render(){
return (
通过context传过来:
<span style={{color:‘blue’}}>{this.context.text}
)
}
}
export default Grandson
通过this.context.text获取到传递的值。
以上的是一个父–>孙的过程,即向下的流程,如果想孙–>父向上传值,可以通过回调的方式
对父组件进行传值修改,在传过来的对象中添加一个属性,里面绑定父组件的方法value={{text:‘good luck’,toParent:this.fromGranson}}
import React from ‘react’;
import Children from ‘./Children’;
import MyContext from ‘./context’;
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg:‘’
};
this.fromGranson = this.fromGranson.bind(this)
}
fromGranson(val){
this.setState({
msg:val
})
}
// 使用一个 Provider 来将当前的 theme 传递给以下的组件树。
// 无论多深,任何组件都能读取这个值。
render(){
return (
context通信实例
<span style={{color:‘red’}}>{this.state.msg}
<MyContext.Provider value={{text:‘good luck’,toParent:this.fromGranson}}>
</MyContext.Provider>
)
}
}
export default Parent
然后在孙组件中添加一个按钮,绑定方法,执行函数回调
toParent(){
this.context.toParent(‘孙组件向父组件传数据’)
}
import React from ‘react’;
import MyContext from ‘./context’;
import { Button } from ‘element-react’
class Grandson extends React.Component {
static contextType = MyContext
constructor(props) {
super(props);
this.toParent = this.toParent.bind(this)
}
toParent(){
this.context.toParent(‘孙组件向父组件传数据’)
}
render(){
return (
通过context传过来:
<span style={{color:‘blue’}}>{this.context.text}
)
}
}
export default Grandson
默认的页面为:
点击按钮之后,执行context中的回调,向上传值。
不管层级有多深,都可以使用context进行向下或向上传值。
注意:在下层组件中取的context中的字段需与value中传递字段保持一致。text与toParent
以上就是Context的大致使用,更多细节请往React官方文档:
Context – Reacthttps://react.docschina.org/docs/context.html
三、兄弟(无嵌套)组件通信
当两个组件互不嵌套,处在同个层级或者不同层级上,他们之间要进行通信,有以下几种常用方法
1、某个组件先将值传到同一个父组件,然后在通过父组件传给另外一个组件,用到父子组件传值
2、使用缓存sessionStorage、localStorage等
3、如果两个组件之间存在跳转,可以使用路由跳转传值,附上详细用法
React学习笔记 – 组件通信之路由传参(react-router-dom)_前端菜小白leo的博客-优快云博客
4、event(发布–订阅)
首先,安装event
npm install event -save
新建一个event.js
import { EventEmitter } from ‘events’;
最后
19224250863)]https://react.docschina.org/docs/context.html](https://react.docschina.org/docs/context.html “Context – React”)
三、兄弟(无嵌套)组件通信
当两个组件互不嵌套,处在同个层级或者不同层级上,他们之间要进行通信,有以下几种常用方法
1、某个组件先将值传到同一个父组件,然后在通过父组件传给另外一个组件,用到父子组件传值
2、使用缓存sessionStorage、localStorage等
3、如果两个组件之间存在跳转,可以使用路由跳转传值,附上详细用法
React学习笔记 – 组件通信之路由传参(react-router-dom)_前端菜小白leo的博客-优快云博客
4、event(发布–订阅)
首先,安装event
npm install event -save
新建一个event.js
import { EventEmitter } from ‘events’;