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工程,目录结 ...
随机推荐
- Spring中@Component与@Bean的区别
@Component和@Bean的目的是一样的,都是注册bean到Spring容器中. @Component VS @Bean @Component 和 它的子类型(@Controller, @S ...
- # win10下设置软件启动快捷方式
win10下设置软件启动快捷方式 win10下设置软件启动快捷键,必须把快捷方式放在C:\ProgramData\Microsoft\Windows\Start Menu\Programs目录下,在这 ...
- RPA自定义脚本打开文件夹
import os import subprocess from rpa.web.common.utils import convert_2_unicode def startfile(filenam ...
- k8s之资源指标API部署metrics-server
1.部署metrics-server 从v1.8开始,引入了新的功能,即把资源指标引入api,资源指标:metrics-server,自定义指标:prometheus,k8s-prometheus-a ...
- IntelliJ IDEA 统一设置编码为utf-8编码
问题一: File->Settings->Editor->File Encodings 问题二: File->Other Settings->Default Settin ...
- 怎样在页面关闭时发起HTTP请求
比如有需求是要让页面关闭时, 在数据库中记录用户的一些数据或log日志. 这时就需要在用户关闭页面时发起HTTP请求. 做法是对window.onunload设置事件监听函数, 在函数内发起AJAX请 ...
- StoneTab标签页CAD插件 3.2.1
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- 关于windows下编写的shell脚本在linux下无法运行报错问题
首先,你写的shell脚本必须是正确的, 其次,无法运行的原因:因为windows下的换行是两个字节,而你上传到linux,linux下换行是两个字节,所以编译的酒不正确的,导致无法 运行脚本, 这种 ...
- 判断两个list是否元素一样
首先创建枚举 public enum TheType { type1 = , type2 = , type3 = } 1.如果不考虑顺序,即顺序不一样,只要元素都一样即可 List<TheTyp ...
- idea的EasyCode使用
EasyCode可以自动根据表格生成:entity,dao,service,serviceImpl,controller 使用方法: 一.安装EasyCode插件: File-setting-Plug ...