1、父子组件通信

1)父组件与子组件通信,使用Props

父组件将name传递给子组件

 <GreateH name="kitty"/>

子组件通过props接收父组件的值,并显示

class GreateH extends React.Component{
static defaultProps = {
name:'CoCo'
};
constructor(props){
super(props);
this.state ={
name:props.name
}
}
render(){
return <div>
<h2>hello,{this.state.name}</h2>
</div>
}
}

2)子组件与父组件通信,执行回调函数



如图所示,点击子组件按钮改变父组件中标题颜色

class GreateH extends React.Component{
static defaultProps = {
name:'CoCo'
};
constructor(props){
super(props);
this.state ={
name:props.name
}
}
changeBtn(){
if(typeof this.props.click == 'function' ){
//触发父组件的事件,改变父组件中标题颜色
this.props.click();
}
};
render(){
return <div>
<h2>hello,{this.state.name}</h2>
<button onClick={this.changeBtn.bind(this)}>点击改变标题颜色</button>
</div>
}
} export default GreateH;

父组件中通过changeColor事件改变对应标题的颜色

class App extends Component {
changeColor(obj){
var oDom = document.getElementsByClassName(obj.class)[0];
oDom.style.color = obj.color;
};
render() {
return (
<div className="App">
<h2 className="title1">子组件一</h2>
<GreateH name="kitty" click={this.changeColor.bind(this,{color:'red',class:'title1'})}/>
<hr/>
<h2 className="title2">子组件二</h2>
<GreateH name="lily" click={this.changeColor.bind(this,{color:'blue',class:'title2'})}/>
</div>
);
}
} export default App;

2、兄弟组件通信

如图所示,要实现点击B组件的按钮改变A的名称,点击A组件的按钮改变B组件的名称

父组件:

class App extends Component {
constructor(props){
super(props);
this.state = {
nameA:'kitty',
nameB:'Lily'
}
}
changeBName(params){
this.setState({
nameB:params
}) }
changeAName(params){
this.setState({
nameA:params
})
}
render() {
return (
<div className="App">
<h2 className="title1">组件A</h2>
<GreateA name={this.state.nameA} click={this.changeBName.bind(this)}/>
<hr/>
<h2 className="title2">组件B</h2>
<GreateB name={this.state.nameB} click={this.changeAName.bind(this)}/>
</div>
);
}
}

A组件:

class GreateH extends React.Component{
static defaultProps = {
name:''
}; changeBtn(){
if(typeof this.props.click == 'function' ){
this.props.click('kristy');
}
};
render(){
return <div>
<h2>hello,{this.props.name}</h2>
<button onClick={this.changeBtn.bind(this)}>点击改变B组件的名字</button>
</div>
}
}

B组件

class GreateH extends React.Component{
static defaultProps = {
name:''
};
changeBtn(){
if(typeof this.props.click == 'function' ){
this.props.click('CoCo');
}
};
render(){
return <div>
<h2>hello,{this.props.name}</h2>
<button onClick={this.changeBtn.bind(this)}>点击改变A组件的名字</button>
</div>
}
}

学到这里有个问题,为什么这样写没有用:

class GreateH extends React.Component{
static defaultProps = {
name:''
};
constructor(props){
super(props);
this.state ={
name:this.props.name
}
}
changeBtn(){
if(typeof this.props.click == 'function' ){
this.props.click('CoCo');
}
};
render(){
return <div>
// 改成this.props.name之后才能检测到变化
<h2>hello,{this.state.name}</h2>
<button onClick={this.changeBtn.bind(this)}>点击改变A组件的名字</button>
</div>
}
}

这个需要加一个钩子函数,在钩子函数中去改变state的值,如下:

  static getDerivedStateFromProps(props,state){
return {
name:props.name
}
}

React 入门学习笔记整理(六)—— 组件通信的更多相关文章

  1. React 入门学习笔记整理目录

    React 入门学习笔记整理(一)--搭建环境 React 入门学习笔记整理(二)-- JSX简介与语法 React 入门学习笔记整理(三)-- 组件 React 入门学习笔记整理(四)-- 事件 R ...

  2. React 入门学习笔记整理(三)—— 组件

    1.定义组件 1)函数组件 function GreateH(props){ return <div> <h2>hello,{props.name}</h2> &l ...

  3. React 入门学习笔记整理(四)—— 事件

    1.事件定义 React事件绑定属性的命名采用驼峰式写法,而不是小写. 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法) 在类组件中定义函数,通过thi ...

  4. React 入门学习笔记整理(五)—— state

    1.state 1)组件本省也是有状态的,定义在组件内部的state中,state的状态只能由组件自身改变,任何其他组件都不能改变. 当需要改变state时,通过调用setState方法来改变,set ...

  5. React 入门学习笔记整理(七)—— 生命周期

    (1)react 生命周期 只有类组件有生命周期,函数组件没有生命周期 1.挂载阶段:这些方法会在组件实例被创建和插入DOM中时被调用: 1)constructor(props) 初始化组件的状态.绑 ...

  6. React 入门学习笔记整理(九)——路由

    (1)安装路由 React-router React-router提供了一些router的核心api,包括Router, Route, Switch等,但是它没有提供dom操作进行跳转的api. Re ...

  7. React 入门学习笔记整理(一)——搭建环境

    使用create-react-app脚手架搭建环境 1.安装node .软件下载地址:https://nodejs.org/en/,我下的推荐的版本. 安装之后测试是否安装成功.windows系统下, ...

  8. React 入门学习笔记整理(二)—— JSX简介与语法

    先看下这段代码: import React from 'react'; //最终渲染需要调用ReactDOM库,将jsx渲染都页面中 import ReactDOM from 'react-dom'; ...

  9. React 入门学习笔记整理(八)—— todoList

    APP.js import React, { Component,createRef,Fragment} from 'react'; import Todos from './components/t ...

随机推荐

  1. jdk8新特性(详解)

    最近在复习外加看点面试题,jdk8的新特性虽然在项目用用到过一两个,准备系统的了解一下jdk8的常用新特性 一:Lambd表达式 也可称为闭包         引入函数式编程到Java中 为了使现有函 ...

  2. js截取字符串方法整理slice(), substr(), substring(), split()

      substr(start,length) stringObject.substr(start,length) //start,截取起始下标,-1 指字符串最后一个字符,-2 指倒数第二个字符开始 ...

  3. 如果你在it院校学习累了,你能干什么?

    文章来源i春秋,未经允许不得转载    工具链接https://bbs.ichunqiu.com/portal.php 如果你在国内的it院校累了,有些厌倦了,你该怎么办?    分享一些joke以前 ...

  4. js生成随机固定长度字符串的简便方法

    概述 碰到一个需求:用js生成固定长度的字符串.在网上查了很多资料,网上的方法都比较麻烦.我自己灵光一现,实现了一个比较简单的方法.记录下来,供以后开发时参考,相信对其他人也有用. js生成随机字符串 ...

  5. virtual box 下安装centos 7

    1: 在virtual box下导入 镜像的时候报错: Failed to open/create the internal network 'HostInterfaceNetworking-Virt ...

  6. 前端自动化部署方案-实践(配合shell)

    以下实例项目为vue项目,其他项目当然也雷同咯 在项目中建一个这个么脚本文件 不说了,上代码 #!/bin/sh handle=$1; env=$2; # 远程部署机 webhook # 如果用远程机 ...

  7. python编译生成的.pyc作用

    如果 Python 进程在机器上拥有写入权限,那么它将把程序的字节码保存为一个以 .pyc 为扩展名的文件( ".pyc" 就是编译过的 ".py" 源代码). ...

  8. HoloLens开发手记 - HoloLens shell概述 HoloLens shell overview

    使用HoloLens时,shell是由你周围的世界和来自系统的全息图像构成.我们将这种空间成为混合世界(mixed world). shell包含了一个可以让你将全息图像和应用放置在世界中的开始菜单( ...

  9. Tomcat简介

    Tomcat简单的说就是一个运行JAVA的网络服务器,底层是Socket的一个程序,它也是JSP和Serlvet的一个容器. 如果你学过html,css,你会知道你写的页面只能自己访问,别人不能远程访 ...

  10. 使用pyenv来管理python版本

    使用pyenv可以很方便的切换python版本,而不会影响系统的python版本,对需要使用supervisor(仅支持python2)托管程序,项目使用python3开发的情况十分有用 pyenv的 ...