1、一个demo(https://www.reactjscn.com/docs/state-and-lifecycle.html)

class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
} componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
} componentWillUnmount() {
clearInterval(this.timerID);
} tick() {
this.setState({
date: new Date()
});
} render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
} ReactDOM.render(
<Clock />,
document.getElementById('root')
);

2、demo: 兄弟组件之间通信,使用到生命周期钩子componentWillReceiveProps

项目结构:

demo效果:“搜索组件”传递keyword参数给父组件,然后父组件将keyword参数传递给“显示子组件”

App.jsx

import React from 'react'
import UserSearch from '../user-search/UserSearch.jsx'
import UserList from '../user-list/UserList.jsx' export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
keyword: ''
} this.setKeyword = this.setKeyword.bind(this)
} setKeyword(keyword) {
console.log(`App组件setKeyword被调用, keyword=${keyword}`)
this.setState({ keyword })
} render() {
return (
<div>
<UserSearch setKeyword={this.setKeyword} />
<UserList keyword={this.state.keyword} />
</div>
)
}
}

  

  UserSearch.jsx

import React from 'react'
import PropTypes from 'prop-types' export default class UserSearch extends React.Component {
state = {
keyword: ''
} static propTypes = {
setKeyword: PropTypes.func.isRequired
} render() {
const { keyword} = this.state
return (
<div>
<h3>搜索用户</h3>
<input type="text" placeholder="请输入搜索关键字" name="keyword" value={keyword} onChange={(e) => this.keywordInputOnChangeHandler(e)} />
<input type="button" value="开始搜索" onClick={() => this.searchClickHandler()} />
</div>
)
} keywordInputOnChangeHandler = (e) => {
const keyword = e.target.value
this.setState({ keyword })
} searchClickHandler = () => {
const { keyword } = this.state
console.log(`keyword.trim()=${keyword.trim()}`)
if (!keyword.trim()) return
this.props.setKeyword(keyword)
}
}

  UserList.jsx

import React from 'react'
import PropTypes from 'prop-types'
import Axios from 'axios' export default class UserList extends React.Component {
state = {
userList: []
} componentWillReceiveProps(newProps) {
const { keyword } = newProps
Axios.get(`http://localhost:4000/react-db-crud/user/search?keyword=${keyword}`)
.then(res => {
if (res.data.code == 0) {
const userList = res.data.data
console.log(`userList=${JSON.stringify(userList)}`)
this.setState({ userList })
}
})
} render() {
return (
<div>
{
this.state.userList.map(user => <p>{user.id + "--" + user.name + "--" + user.age}</p>)
}
</div>
)
}
} UserList.propTypes = {
keyword: PropTypes.string.isRequired
}

React生命周期, 兄弟组件之间通信的更多相关文章

  1. react复习总结(2)--react生命周期和组件通信

    这是react项目复习总结第二讲, 第一讲:https://www.cnblogs.com/wuhairui/p/10367620.html 首先我们来学习下react的生命周期(钩子)函数. 什么是 ...

  2. 在npm Vue单页面的环境下,兄弟组件之间通信Bus

    参考1:https://www.cnblogs.com/wangruifang/p/7772631.html 参考2:https://www.jianshu.com/p/b3d09c6c87bf 在m ...

  3. react生命周期和组件生命周期

    React的组件在第一次挂在的时候首先获取父组件传递的props,接着获取初始的state值,接着经历挂载阶段的三个生命周期函数,也就是ComponentWillMount,render,Compon ...

  4. vue工程利用pubsub-js实现兄弟组件之间的通信

    前言 项目是基于vue-cli创建的,不会搭建vue开发环境的同学可以百度,这里不再赘述. 步骤流程 vue项目搭建完成之后的文件图如下: 我的上一篇博客已经详细叙述vue工程中各个文件的作用,不清楚 ...

  5. React 组件之间通信 All in One

    React 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 props 2. 兄弟组件之间通信 3. 跨多层级的组件之间通信 Context API https://react ...

  6. Vue 组件之间通信 All in One

    Vue 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 https://stackblitz.com/edit/vue-parent-child-commutation?fil ...

  7. React中组件之间通信的方式

    一.是什么 我们将组件间通信可以拆分为两个词: 组件 通信 回顾Vue系列的文章,组件是vue中最强大的功能之一,同样组件化是React的核心思想 相比vue,React的组件更加灵活和多样,按照不同 ...

  8. 使用EventBus实现兄弟组件之间的通信

    使用EventBus实现兄弟组件之间的通信 需求:为了实现菜单折叠的效果,例如http://blog.gdfengshuo.com/example/work/#/dashboard header组件和 ...

  9. Vue2.0父子组件之间和兄弟组件之间的数据交互

    熟悉了Vue.js的同级组件之间通信,写此文章,以便记录. Vue是一个轻量级的渐进式框架,对于它的一些特性和优点,请在官网上进行查看,不再赘述. 使用NPM及相关命令行工具初始化的Vue工程,目录结 ...

随机推荐

  1. Hadoop学习(4)-mapreduce的一些注意事项

    关于mapreduce的一些注意细节 如果把mapreduce程序打包放到了liux下去运行, 命令java  –cp  xxx.jar 主类名 如果报错了,说明是缺少相关的依赖jar包 用命令had ...

  2. Redis 学习笔记(篇八):事件

    Redis 服务器是一个事件驱动程序,服务器需要处理以下两类事件: 文件事件: Redis 服务器通过套接字与客户端(或者其他 Redis 服务器)进行连接,而文件事件就是服务器对套接字操作的抽象.服 ...

  3. Chrome浏览器控制台报 POST http://*** net::ERR_BLOCKED_BY_CLIENT

    开发项目广告模块时,遇到前端提交的请求后台拿不到,好像被什么拦截了,查看了过滤器,拦截器都无错误,且请求也到不了拦截器,chrome浏览器报:ERR_BLOCKED_BY_CLIENT错误 搞腾一半天 ...

  4. 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)

    --喜欢记得关注我哟[shoshana]-- 目录 1.朴素的模式匹配算法2.KMP模式匹配算法 2.1 KMP模式匹配算法的主体思路 2.2 next[]的定义与求解 2.3 KMP完整代码 2.4 ...

  5. 剪花布条 HDU - 2087(kmp,求不重叠匹配个数)

    Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入 ...

  6. 剑指offer37:统计一个数字在排序数组中出现的次数

    1 题目描述 统计一个数字在排序数组中出现的次数. 2 思路和方法 (1)查找有序数组,首先考虑使用二分查找,使时间复杂度为O(log n).更改二分查找的条件,不断缩小区间,直到区间头和区间尾均为k ...

  7. git 使用2

    安装 1.下载对应版本:https://git-scm.com/download 2.安装git:在选取安装路径的下一步选取 Use a TrueType font in all console wi ...

  8. MySQL create table语法中的key与index的区别

    在create table的语句中,key和index混淆在一起,官方手册中的解释是这样: KEY is normally a synonym for INDEX. The key attribute ...

  9. OkHttp3 + retrofit2 封装

    0.下载文件 1.gradle 添加 compile 'com.squareup.retrofit2:retrofit:2.1.0'compile 'com.squareup.retrofit2:co ...

  10. php 测试php连接redis集群的案例

    <?php$redis_list = ['12.24.18.2:6379'];$client = new RedisCluster(NUll,$redis_list);echo $client- ...