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. SQL优化记录

    2019.06.19记录: 1.SQL优化的原因: 原因:性能低,执行时间太长,等待时间太长,SQL语句欠佳(尤其连接查询),索引失效,服务器参数设置的不合理(如:缓冲区,线程等) a.SQL: 编写 ...

  2. PHP max_input_var设为了1000导致post数组太多时无法接受后面的参数值

    PHP max_input_var设为了1000导致post数组太多时无法接受后面的参数值 下午突然接到格力电话说无法批量设置门店任务,但是在测试环境下无法重现,测试环境下好好的. 然后登陆到生产环境 ...

  3. selenium登录4399

    from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...

  4. 客户端相关知识学习(九)之h5给app传递数据

    方法一: 情况一: if (window.JdAndroid){          window.JdAndroid.setPayCompleted();          window.JdAndr ...

  5. linux 打包与解压命令--常用

    一般情况用这俩个就足以了 压缩 tar -czf jpg.tar.gz *.jpg   //将目录里所有jpg文件打包成jpg.tar后,并且将其用gzip压缩,生成一个gzip压缩过的包,命名为jp ...

  6. Subplots

    数据读取: Subplotting 先展示下我们在画一张图时的步骤 生成一个matplotlib Figure对象 生成一个matplotlib AxesSubplot 对象,然后将其赋值给Figur ...

  7. java中数组的定义

    1. 一维数组 int[] arr = new int[3];//需要一个容器,但是暂时不给具体的数值 int[] arr = new int[3]{1,2,3};//直接给定具体数值 int[] a ...

  8. Outline 科学的上网

    outline 官网:https://getoutline.org/zh-CN/home 下载 Outline 管理器 下载 Outline 客户端 配置浏览器代理

  9. CentOS7中使用yum安装Nginx的详细步骤

    1.添加源 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarc ...

  10. Eclipse创建Maven项目时,项目中只存在src/main/resources(没有src/main/java、src/test/java)的解决方法

      例:Maven项目(chapter11),发现只存在src/main/resources,缺少了src/main/java和src/test/java 解决方法: 1.eclipse->wi ...