React生命周期, 兄弟组件之间通信
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生命周期, 兄弟组件之间通信的更多相关文章
- react复习总结(2)--react生命周期和组件通信
这是react项目复习总结第二讲, 第一讲:https://www.cnblogs.com/wuhairui/p/10367620.html 首先我们来学习下react的生命周期(钩子)函数. 什么是 ...
- 在npm Vue单页面的环境下,兄弟组件之间通信Bus
参考1:https://www.cnblogs.com/wangruifang/p/7772631.html 参考2:https://www.jianshu.com/p/b3d09c6c87bf 在m ...
- react生命周期和组件生命周期
React的组件在第一次挂在的时候首先获取父组件传递的props,接着获取初始的state值,接着经历挂载阶段的三个生命周期函数,也就是ComponentWillMount,render,Compon ...
- vue工程利用pubsub-js实现兄弟组件之间的通信
前言 项目是基于vue-cli创建的,不会搭建vue开发环境的同学可以百度,这里不再赘述. 步骤流程 vue项目搭建完成之后的文件图如下: 我的上一篇博客已经详细叙述vue工程中各个文件的作用,不清楚 ...
- React 组件之间通信 All in One
React 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 props 2. 兄弟组件之间通信 3. 跨多层级的组件之间通信 Context API https://react ...
- Vue 组件之间通信 All in One
Vue 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 https://stackblitz.com/edit/vue-parent-child-commutation?fil ...
- React中组件之间通信的方式
一.是什么 我们将组件间通信可以拆分为两个词: 组件 通信 回顾Vue系列的文章,组件是vue中最强大的功能之一,同样组件化是React的核心思想 相比vue,React的组件更加灵活和多样,按照不同 ...
- 使用EventBus实现兄弟组件之间的通信
使用EventBus实现兄弟组件之间的通信 需求:为了实现菜单折叠的效果,例如http://blog.gdfengshuo.com/example/work/#/dashboard header组件和 ...
- Vue2.0父子组件之间和兄弟组件之间的数据交互
熟悉了Vue.js的同级组件之间通信,写此文章,以便记录. Vue是一个轻量级的渐进式框架,对于它的一些特性和优点,请在官网上进行查看,不再赘述. 使用NPM及相关命令行工具初始化的Vue工程,目录结 ...
随机推荐
- MySQL添加、修改、撤销用户数据库操作权限的一些记录
查看MYSQL数据库中所有用户 SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; ...
- JS利用async、await处理少见的登录业务逻辑
在用uniapp开发一个项目时,有这样一个需求:用户首次登录后,uniapp自动保存用户名密码,之后不管是再次打开项目(打开项目时登录状态已失效)还是 请求接口(接口返回登录失效)时,都会先自动默认的 ...
- SQL0668N Operation not allowed for reason code "3" on table "TEST". SQLSTATE=57016
问题描述: 查询,操作表都报如下错误 SQL0668N Operation not allowed for reason code "3" on table "TEST ...
- 【51nod】2606 Secondary Substring
51nod 2606 Secondary Substring 感觉有趣的一道计数,实际上不难 感觉好久没用这种技巧了,导致我还在错误的道路上想了好久... 观察题目性质,可以发现就是左边第一次出现两遍 ...
- thinkphp5.1中使用Bootstrap4分页样式修改
1.找到thinkphp下的Boorstrap的源码 \thinkphp\library\think\paginator\driver\Bootstrap.php 2丶直接修改源码 <?php ...
- 码云以及Git的使用
码云以及Git的使用 码云就是一个远程管理的仓库,Git是用来上传和下载数据的工具. 首先访问网站 https://gitee.com/ 进行注册 注册完成后,进入如下页面 点击新建仓库 设置自己的仓 ...
- Error starting daemon: error initializing graphdriver: devmapper: Device docker-thinpool is not a thin pool
Error starting daemon: error initializing graphdriver: devmapper: Device docker-thinpool is not a th ...
- 怎样终止HTTP请求
使用 xhr.abort() var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.example.com/page.php', tr ...
- C#基础知识 (转)
https://www.cnblogs.com/zhouzhou-aspnet/articles/2591596.html(原文地址) 本文是一个菜鸟所写,本文面向的人群就是像我这样的小菜鸟,工作一年 ...
- VS Code 运行 JavaScript 文件时出现“node...”乱码或错误
1.错误图片: 2.如果是中文乱码的话,可以到设置里边把「Auto Guess Encoding」这一项勾起来. 3.如果不是这个原因,可能是因为没安装 Node.js 和配置 Node.js 环境, ...