• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > 简单常用技巧之React组件间通信(整理分享)

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

作者:站长图库 字体:[增加 减小] 来源:互联网

站长图库向大家介绍了React常用技巧,React组件间通信等相关知识,希望对您有所帮助

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


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


一、父子组件通信

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

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

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

import React from 'react';import { Button } from 'element-react';import Children from './Children'; class Parent extends React.Component {  constructor(props) {    super(props);    this.state = {        msg:'父组件传递给子组件'    };    this.changeMsg = this.changeMsg.bind(this)  }  changeMsg(){    this.setState({      msg:'父组件传递给子组件(改变之后的内容)'    })  }  render(){    return (      <div style={{backgroundColor:'#f7ba2a',padding:'20px',width:'500px',margin:'auto',textAlign:'center'}}>        <p>父子组件通信实例</p>        <Button onClick={this.changeMsg}>父传子</Button>        <Children toChildren={this.state.msg}></Children>      </div>    )  }} export default Parent

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

import React from 'react'; class Children extends React.Component {  constructor(props) {    super(props);    this.state = {        msg:this.props.toChildren   //通过props拿到父组件传过来的值    };  }  render(){    return (      <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>        <p>从父组件传过来:</p>        <span style={{color:'blue'}}>{this.state.msg}</span>      </div>    )  }} export default Children


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


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


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

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


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

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

import React from 'react';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 (      <div style={{backgroundColor:'#f7ba2a',padding:'20px',width:'500px',margin:'auto',textAlign:'center'}}>        <p>父子组件通信实例</p>        <span style={{color:'red'}}>{this.state.fromChildrn}</span>        <Children toChildren={this.state.msg} callback={this.changeMsg}></Children>      </div>    )  }} 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 (      <div style={{backgroundColor:'#13ce66',padding:'10px',width:'200px',margin:'auto',marginTop:'20px'}}>        <p>从父组件传过来:</p>        <span style={{color:'blue'}}>{this.state.msg}</span>        <Button onClick={this.toParent}>子传父</Button>      </div>    )  }} export default Children

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


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


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


二、跨级组件通信

假设一个父组件中存在一个子组件,这个子组件中又存在一个子组件,暂且

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 帝国CMS万能标签调用指定时间内容方法
  • WordPress如何插入视频?WP文章插入视频代码方法
  • 聊聊TP在app接口开发过程中的安全验证问题
  • Photoshop技巧教程:解读13条PS高级使用技巧
  • vue.js怎么实现验证码
  • Javascript怎么实现字符串替换星号
  • Photoshop制作卡通风格的3D立体字教程
  • Illustrator绘制超酷效果的立体字教程
  • Photoshop技巧:CC版本的最全总结
  • 解决WordPress评论加强:防止游客冒充站长回复别人的办法

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • 利用html实现进度条效果的方法
    • php怎么将ppt转jpg图片
    • 小程序swiper轮播CSS3动画及跳转到指定swiper-item的使用
    • 如何实现由抖音、快手、知乎、短信等外部APP或浏览器跳转到微信?
    • Photoshop设计游戏简洁风格的标志教程
    • 帝国CMS后台添加关键字时自动复制到TAGS(同步更新)的方法
    • php如何实现图片上传的封装
    • Photoshop绘制质感的金色水滴视频教程
    • 手把手教你使用Vue3实现图片散落效果
    • Photoshop制作一个水晶卡通人像图标

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有