简单常用技巧之React组件间通信(整理分享)

 3049

本篇文章给大家带来了React组件间通信简单易用的常用方式,React知识中一个主要内容便是组件之间的通信,以下列举几种常用的组件通信方式,结合实例,通俗易懂,建议收藏,希望对大家有帮助。


简单常用技巧之React组件间通信(整理分享)


一、父子组件通信

原理:父组件通过props(与vue中的props区分开)向子组件通信,子组件通过回调事件与父组件通信。

首先,先创建一个父组件Parent.js跟子组件Children.js,二者的关系为直接父子关系。

Parent.js父组件如下,给父组件一个默认状态state,引入子组件,通过在子组件加上toChildren={this.state.msg},该处即为向子组件传props。

  1. import React from 'react';
  2. import { Button } from 'element-react';
  3. import Children from './Children';
  4.  
  5. class Parent extends React.Component {
  6.   constructor(props) {
  7.     super(props);
  8.     this.state = {
  9.         msg:'父组件传递给子组件'
  10.     };
  11.     this.changeMsg = this.changeMsg.bind(this)
  12.   }
  13.   changeMsg(){
  14.     this.setState({
  15.       msg:'父组件传递给子组件(改变之后的内容)'
  16.     })
  17.   }
  18.   render(){
  19.     return (
  20.       <div style={{backgroundColor:'#f7ba2a',padding:'20px',width:'500px',margin:'auto',textAlign:'center'}}>
  21.         <p>父子组件通信实例</p>
  22.         <Button onClick={this.changeMsg}>父传子</Button>
  23.         <Children toChildren={this.state.msg}></Children>
  24.       </div>
  25.     )
  26.   }
  27. }
  28.  
  29. export default Parent

Children.js子组件如下,初始状态通过props拿到父组件传过来的值。

  1. import React from 'react';
  2.  
  3. class Children extends React.Component {
  4.   constructor(props) {
  5.     super(props);
  6.     this.state = {
  7.         msg:this.props.toChildren   //通过props拿到父组件传过来的值
  8.     };
  9.   }
  10.   render(){
  11.     return (
  12.       <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>
  13.         <p>从父组件传过来:</p>
  14.         <span style={{color:'blue'}}>{this.state.msg}</span>
  15.       </div>
  16.     )
  17.   }
  18. }
  19.  
  20. export default Children


简单常用技巧之React组件间通信(整理分享)


注意:子组件取值时应与父组件放在子组件的字段props一致,即本例中的 toChildren,如下


简单常用技巧之React组件间通信(整理分享)

简单常用技巧之React组件间通信(整理分享)


那么子组件想向父组件传值(向上传值),可以通过调用父组件传过来的回调函数

在Parent.js中向Children.js中加入回调函数callback,绑定changeMsg方法

  1. import React from 'react';
  2. import Children from './Children';
  3.  
  4. class Parent extends React.Component {
  5.   constructor(props) {
  6.     super(props);
  7.     this.state = {
  8.         msg:'父组件传递给子组件',
  9.         fromChildrn:''
  10.     };
  11.     this.changeMsg = this.changeMsg.bind(this)
  12.   }
  13.   changeMsg(val){
  14.     this.setState({
  15.       fromChildrn: val
  16.     })
  17.   }
  18.   render(){
  19.     return (
  20.       <div style={{backgroundColor:'#f7ba2a',padding:'20px',width:'500px',margin:'auto',textAlign:'center'}}>
  21.         <p>父子组件通信实例</p>
  22.         <span style={{color:'red'}}>{this.state.fromChildrn}</span>
  23.         <Children toChildren={this.state.msg} callback={this.changeMsg}></Children>
  24.       </div>
  25.     )
  26.   }
  27. }
  28.  
  29. export default Parent

在子组件中,用this.props.callback()执行父组件的回调函数,从而执行绑定方法changeMsg,显示子组件传过来的值

  1. import React from 'react';
  2. import { Button } from 'element-react';
  3.  
  4. class Children extends React.Component {
  5.   constructor(props) {
  6.     super(props);
  7.     this.state = {
  8.         msg:this.props.toChildren
  9.     };
  10.     this.toParent = this.toParent.bind(this)
  11.   }
  12.   toParent(){
  13.     this.props.callback('子组件传过来的值')   //子组件通过此触发父组件的回调方法
  14.   }
  15.   render(){
  16.     return (
  17.       <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>
  18.         <p>从父组件传过来:</p>
  19.         <span style={{color:'blue'}}>{this.state.msg}</span>
  20.         <Button onClick={this.toParent}>子传父</Button>
  21.       </div>
  22.     )
  23.   }
  24. }
  25.  
  26. export default Children

注意:props中的回调函数名称需一致,即本例中的callback,如下


简单常用技巧之React组件间通信(整理分享)
简单常用技巧之React组件间通信(整理分享)


小结: 以上为直接父子组件通信的其中一种方式,父传子,通过props;子传父,执行回调。


二、跨级组件通信

假设一个父组件中存在一个子组件,这个子组件中又存在一个子组件,暂且称为“孙组件”,当父组件需要与“孙组件”通信时,常用的方式有两种,逐层传值与跨层传值。

1、逐层传值

这种方式就是上面的直接父子通信的基础上在加上一个中间层。如父、“孙”组件通信,可以先父子通信,然后再子“孙”通信,传递的层级变成父-->子-->“孙”,同理,通过props往下传,通过回调往上传。不展开,有兴趣的自己动手实现一下。

2、跨级传值

顾名思义,父跟“孙”通信,不需要经过子(中间层)组件。这里引出了Context。

React官方文档对Context做出了解释:

在一个典型的 React 应用中,数据是通过 props 属性自上而下(由父及子)进行传递的,但这种做法对于某些类型的属性而言是极其繁琐的(例如:地区偏好,UI 主题),这些属性是应用程序中许多组件都需要的。Context 提供了一种在组件之间共享此类值的方式,而不必显式地通过组件树的逐层传递 props。

一句话概括就是:跨级传值,状态共享

看下简单的实例,直接讲用法。

首先,我先创建一个context.js文件(与父子孙同个目录),默认值为一个对象。

  1. import React from "react";
  2. const MyContext = React.createContext({text:'luck'});
  3. export default MyContext

然后,对父组件进行改写,引入context,使用一个 Provider 来将当前的 value 传递给以下的组件树,value为传递的值。

  1. import React from 'react';
  2. import Children from './Children';
  3. import MyContext from './context';
  4.  
  5. class Parent extends React.Component {
  6.   constructor(props) {
  7.     super(props);
  8.   }
  9.   // 使用一个 Provider 来将当前的 value 传递给以下的组件树。
  10.   // 无论多深,任何组件都能读取这个值。
  11.   render(){
  12.     return (
  13.       <div style={{backgroundColor:'#f7ba2a',padding:'20px',width:'500px',margin:'auto',textAlign:'center'}}>
  14.         <p>context通信实例</p>
  15.         <MyContext.Provider value={{text:'good luck'}}>
  16.           <Children></Children>
  17.         </MyContext.Provider>
  18.       </div>
  19.     )
  20.   }
  21. }
  22.  
  23. export default Parent

子组件为中间层,不做处理,用于包裹“孙”组件。

  1. import React from 'react';
  2. import Grandson from './Grandson';
  3.  
  4. class Children extends React.Component {
  5.   render(){
  6.     return (
  7.       <div>
  8.         <Grandson></Grandson>
  9.       </div>
  10.     )
  11.   }
  12. }
  13.  
  14. export default Children

新增一个“孙”组件,同样需引入context,在组件内部添加static contextType = MyContext,此时将能通过this.context直接获取到上层距离最近的Provider传递的值,此时this.context = {text:good luck},即父组件传递value。

  1. import React from 'react';
  2. import MyContext from './context';
  3.  
  4. class Grandson extends React.Component {
  5.   static contextType = MyContext
  6.   render(){
  7.     return (
  8.       <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>
  9.         <p>通过context传过来:</p>
  10.         <span style={{color:'blue'}}>{this.context.text}</span>
  11.       </div>
  12.     )
  13.   }
  14. }
  15.  
  16. export default Grandson

通过this.context.text获取到传递的值。


简单常用技巧之React组件间通信(整理分享)


以上的是一个父-->孙的过程,即向下的流程,如果想孙-->父向上传值,可以通过回调的方式

对父组件进行传值修改,在传过来的对象中添加一个属性,里面绑定父组件的方法value={undefined{text:'good luck',toParent:this.fromGranson}}

  1. import React from 'react';
  2. import Children from './Children';
  3. import MyContext from './context';
  4.  
  5. class Parent extends React.Component {
  6.   constructor(props) {
  7.     super(props);
  8.     this.state = {
  9.       msg:''
  10.     };
  11.     this.fromGranson = this.fromGranson.bind(this)
  12.   }
  13.   fromGranson(val){
  14.     this.setState({
  15.       msg:val
  16.     })
  17.   }
  18.   // 使用一个 Provider 来将当前的 theme 传递给以下的组件树。
  19.   // 无论多深,任何组件都能读取这个值。
  20.   render(){
  21.     return (
  22.       <div style={{backgroundColor:'#f7ba2a',padding:'20px',width:'500px',margin:'auto',textAlign:'center'}}>
  23.         <p>context通信实例</p>
  24.         <span style={{color:'red'}}>{this.state.msg}</span>
  25.         <MyContext.Provider value={{text:'good luck',toParent:this.fromGranson}}>
  26.           <Children></Children>
  27.         </MyContext.Provider>
  28.       </div>
  29.     )
  30.   }
  31. }
  32.  
  33. export default Parent

然后在孙组件中添加一个按钮,绑定方法,执行函数回调

  1. toParent(){undefined
  2.     this.context.toParent('孙组件向父组件传数据')
  3. }
  1. import React from 'react';
  2. import MyContext from './context';
  3. import { Button } from 'element-react'
  4.  
  5. class Grandson extends React.Component {
  6.   static contextType = MyContext
  7.   constructor(props) {
  8.         super(props);
  9.     this.toParent = this.toParent.bind(this)
  10.     }
  11.   toParent(){
  12.     this.context.toParent('孙组件向父组件传数据')
  13.   }
  14.   render(){
  15.     return (
  16.       <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>
  17.         <p>通过context传过来:</p>
  18.         <span style={{color:'blue'}}>{this.context.text}</span>
  19.         <div><Button onClick={this.toParent}>context向上</Button></div>
  20.       </div>
  21.     )
  22.   }
  23. }
  24.  
  25. export default Grandson

默认的页面为:


简单常用技巧之React组件间通信(整理分享)


点击按钮之后,执行context中的回调,向上传值。


简单常用技巧之React组件间通信(整理分享)


不管层级有多深,都可以使用context进行向下或向上传值。

注意:在下层组件中取的context中的字段需与value中传递字段保持一致。text与toParent


简单常用技巧之React组件间通信(整理分享)
简单常用技巧之React组件间通信(整理分享)


以上就是Context的大致使用,更多细节请往React官方文档:

Context – React

https://react.docschina.org/docs/context.html


三、兄弟(无嵌套)组件通信

当两个组件互不嵌套,处在同个层级或者不同层级上,他们之间要进行通信,有以下几种常用方法

1、某个组件先将值传到同一个父组件,然后在通过父组件传给另外一个组件,用到父子组件传值

2、使用缓存sessionStorage、localStorage等

3、如果两个组件之间存在跳转,可以使用路由跳转传值,附上详细用法

React学习笔记 -- 组件通信之路由传参(react-router-dom)

4、event(发布--订阅)

首先,安装event

  1. npm install event -save

新建一个event.js

  1. import { EventEmitter } from 'events';
  2. export default new EventEmitter();

然后另两个组件处于同层级(不同个父组件或者不同层级都可以)

  1. import React from 'react';
  2. import Grandson from './Grandson';
  3. import GrandsonOther from './GrandsonOther';
  4.  
  5. class Children extends React.Component {
  6.   render(){
  7.     return (
  8.       <div>
  9.         <Grandson></Grandson>
  10.         <GrandsonOther></GrandsonOther>
  11.       </div>
  12.     )
  13.   }
  14. }
  15.  
  16. export default Children

组件一,导入event,在componentDidMount阶段添加监听addListener(订阅),在componentWillUnmount移除监听removeListener,事件名称与组件二中emit一致。

  1. import React from 'react';
  2. import event from '../event';
  3.  
  4. class Grandson extends React.Component {
  5.   constructor(props) {
  6. super(props);
  7.     this.state = {
  8.       msg:''
  9.     }
  10.   }
  11.   componentDidMount(){
  12.     event.addListener('eventMsg',val => {
  13.       this.setState({
  14.         msg:val
  15.       })
  16.     })
  17.   }
  18.   componentWillUnmount(){
  19.     event.removeListener('eventMsg')
  20.   }
  21.   render(){
  22.     return (
  23.       <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>
  24.         <p>组件一</p>
  25.         <p>通过event传过来:</p>
  26.         <span style={{color:'red'}}>{this.state.msg}</span>
  27.       </div>
  28.     )
  29.   }
  30. }
  31.  
  32. export default Grandson

组件二,导入event,按钮绑定方法,使用event.emit触发(发布)事件。

  1. import React from 'react';
  2. import event from '../event';
  3. import { Button } from 'element-react'
  4.  
  5. class Grandson extends React.Component {
  6.   constructor(props) {
  7. super(props);
  8.     this.state = {
  9.       msg:''
  10.     }
  11.     this.toOther = this.toOther.bind(this)
  12.   }
  13.   toOther(){
  14.     event.emit('eventMsg','通过evnet传过来的值')
  15.   }
  16.   render(){
  17.     return (
  18.       <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>
  19.         <p>组件二</p>
  20.         <span style={{color:'blue'}}>{this.state.msg}</span>
  21.         <div><Button onClick={this.toOther}>event传值</Button></div>
  22.       </div>
  23.     )
  24.   }
  25. }
  26.  
  27. export default Grandson

点击按钮,组件二发布事件,组件一监听(订阅)事件,更新内容。(如果交换发布者订阅者身份,写法一致)


简单常用技巧之React组件间通信(整理分享)
简单常用技巧之React组件间通信(整理分享)

注意:如果两个组件使用event进行通信,确保发布订阅的事件名称一致,如上例中 eventMsg

小结: event的方式比较灵活,不管是父子、跨级、还是同级,甚至毫无关联的组件,都可以使用此方式进行通信。


四、路由传值

React学习笔记 -- 组件通信之路由传参(react-router-dom)


五、Redux

Redux基本用法(在react中使用,链路打通)

总结:主要讲了react中常用的组件通信方式,在平时工作中,根据不同的应用场景,选择不同的通信方式,会让通信流程更加简单、清晰。

对比Vue中的组件通信方式,你会发现很多相似之处:

Vue组件间的通信方式(多种场景,通俗易懂,建议收藏)


本文网址:https://www.zztuku.com/index.php/detail-10895.html
站长图库 - 简单常用技巧之React组件间通信(整理分享)
申明:本文转载于《CSDN》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐