React 入门学习笔记整理(六)—— 组件通信
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 入门学习笔记整理(六)—— 组件通信的更多相关文章
- React 入门学习笔记整理目录
React 入门学习笔记整理(一)--搭建环境 React 入门学习笔记整理(二)-- JSX简介与语法 React 入门学习笔记整理(三)-- 组件 React 入门学习笔记整理(四)-- 事件 R ...
- React 入门学习笔记整理(三)—— 组件
1.定义组件 1)函数组件 function GreateH(props){ return <div> <h2>hello,{props.name}</h2> &l ...
- React 入门学习笔记整理(四)—— 事件
1.事件定义 React事件绑定属性的命名采用驼峰式写法,而不是小写. 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法) 在类组件中定义函数,通过thi ...
- React 入门学习笔记整理(五)—— state
1.state 1)组件本省也是有状态的,定义在组件内部的state中,state的状态只能由组件自身改变,任何其他组件都不能改变. 当需要改变state时,通过调用setState方法来改变,set ...
- React 入门学习笔记整理(七)—— 生命周期
(1)react 生命周期 只有类组件有生命周期,函数组件没有生命周期 1.挂载阶段:这些方法会在组件实例被创建和插入DOM中时被调用: 1)constructor(props) 初始化组件的状态.绑 ...
- React 入门学习笔记整理(九)——路由
(1)安装路由 React-router React-router提供了一些router的核心api,包括Router, Route, Switch等,但是它没有提供dom操作进行跳转的api. Re ...
- React 入门学习笔记整理(一)——搭建环境
使用create-react-app脚手架搭建环境 1.安装node .软件下载地址:https://nodejs.org/en/,我下的推荐的版本. 安装之后测试是否安装成功.windows系统下, ...
- React 入门学习笔记整理(二)—— JSX简介与语法
先看下这段代码: import React from 'react'; //最终渲染需要调用ReactDOM库,将jsx渲染都页面中 import ReactDOM from 'react-dom'; ...
- React 入门学习笔记整理(八)—— todoList
APP.js import React, { Component,createRef,Fragment} from 'react'; import Todos from './components/t ...
随机推荐
- 在源文件(.c)和头文件(.h)中声明和定义的区别——C语言
最近在看多文件编程的时候遇到的一个问题,本来以为理解了声明和定义的区别(然而并没有····),也算是重新认识了一次声明和定义,下面上代码 情形一:在源文件(.c)中 相信大部分读者对声明和定义的理解是 ...
- getaddrinfo 报错 Invalid value for ai_flags
最近改了游戏的网络层代码,运行 Android 版的时候 getaddrinfo 报错 Invalid value for ai_flags. ai_flags 设置如下: struct addrin ...
- 2018CCPC-女生专场
(咕咕咕,咕了快一年的bu题.. A.CCPC直播 传送:http://acm.hdu.edu.cn/showproblem.php?pid=6297 题意:rt. 分析:模拟. #include&l ...
- 腾讯云播放器更新——TCplayer
概述 最近腾讯云播放器进行了更新,增加了TCplayer,支持点播播放.由于工作需要,了解了一下TCplayer,把心得记录下来,供以后开发时参考,相信对其他人也有用. 参考文档: TCPlayer开 ...
- 浅谈css3长度单位rem,以及移动端布局技巧
rem是什么? rem是css3中新增加的一个单位属性(font size of the root element),根据页面的根节点的字体大小进行转变的单位.root!!!!!!!!!根节点,也就是 ...
- Kubernetes-1
master 节点负责管理整个集群,管理的控制面板,全局的角色和调度 3个组件 API Server : 统一入口 kubectl 客户端管理工具 Etcd 数据库 Scheduler 集群的调度 C ...
- PHP-1安装配置
php-fpm启动 /usr/local/php/sbin/php-fpm start
- 修改hosts文件用来观看coursera视频
52.84.246.90 d3c33hcgiwev3.cloudfront.net 52.84.246.252 d3c33hcgiwev3.cloudfront.net 52.84.246.144 d ...
- HoloLens开发手记-配置开发环境 Install the tools
随着Build 2016开发者大会的结束,HoloLens开发包也正式开放下载.Hololens没有独立的SDK,开发特性被集成到最新的Visual Studio Update 2中.如果你没有Hol ...
- 一个关于margin-top的问题
两个 此时内部div的样式为 当我把margin选中 如图所示: 我想要的效果是子div离父div有一个20px的间隙,但显然现在不是我想要的结果, 然后就开始查资料: 这个“问题”……它是CSS2. ...