react列表中,当key改变后发生的事情
Main.jsx
export default class Main extends PureComponent {
constructor(props) {
super(props);
this.state = {
list: [
{
key: ,
value:
},
{
key: ,
value:
},
{
key: ,
value:
}
]
}
this.setKey = ::this.setKey;
}
componentWillMount() {
console.log('main will mount');
}
componentDidMount() {
console.log('main did mount');
}
componentWillUpdate() {
console.log('main will update');
}
componentDidUpdate() {
console.log('main did update');
}
componentWillUnmount() {
console.log('main will unmount');
}
setKey() {
const { list } = this.state;
this.setState({
list: Array.from(list, item => {
return Object.assign(item, {
key: item.key +
});
})
});
}
render() {
const { list } = this.state;
return (
<div>
{list.map(item => <List key={item.key} value={item.value} />)}
<button onClick={this.setKey}>key</button>
</div>
)
}
}
List.jsx
export default class List extends PureComponent {
constructor(props) {
super(props);
}
componentWillMount() {
console.log(`list${this.props.value} will mount`);
}
componentDidMount() {
console.log(`list${this.props.value} did mount`);
}
componentWillUpdate() {
console.log(`list${this.props.value} will update`);
}
componentDidUpdate() {
console.log(`list${this.props.value} did update`);
}
componentWillUnmount() {
console.log(`list${this.props.value} will unmount`);
}
render() {
const { value } = this.props;
return(
<div>
list{value}
</div>
)
}
}
当点击key按钮后会发生什么呢?先分析一下

react列表中,当key改变后发生的事情的更多相关文章
- React组件中的key
React组件中的key 一.key的作用 react中的key属性,它是一个特殊的属性,它是出现不是给开发者用的(例如你为一个组件设置key之后不能获取组件的这个key props),而是给reac ...
- react项目中引入了redux后js控制路由跳转方案
如果你的项目中并没有用到redux,那本文你可以忽略 问题引入 纯粹的单页面react应用中,通过this.props.history.push('/list')就可以进行路由跳转,但是加上了redu ...
- 列表中相同key的字典相加
# 怎么把列表中相同key的字典相加,也就是id的值加id的值,doc_count的值加doc_count的值 # 目标列表 l=[{'id': 5, 'doc_count': 129}, {'id' ...
- .Net中使用com组件后发生System.ArithmeticException异常的解决办法(Message=算术运算中发生溢出或下溢。)
最近在开发一个.Net程序,其中涉及到对com组件的调用,或者第三方DLL调用, 在调用完以后如果使用一些小的测试程序继续运行,一切正常,但是在使用带有GUI的form程序,或者WPF程序中,继续执行 ...
- 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。
使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...
- 浏览器输入url后发生的事情以及每步可以做的优化
首先总结下输入url按下回车后的大致流程: 查询url的ip地址. 建立tcp连接,连接服务器. 浏览器发起http/https请求. 服务器响应浏览器的请求. 网页的解析与渲染. 下面分析每个过程 ...
- React之事件绑定、列表中key的使用
在学习React的Hadding Events这一章节,发现事件回调函数的几种写法,看似区别不大,但实际差异还是蛮大的. class Toggle extends React.Component{ c ...
- 写 React / Vue 项目时为什么要在列表组件中写 key,其作用是什么
怼一波,在项目中有很多经常用到,但又含糊不清的知识点 框架中的key: 1. 为啥在遍历元素时要用 key :在开发过程中为了保证遍历同级元素的唯一性,用来提高更新 dom 的性能: 2. 凭啥要保证 ...
- React 点击删除列表中对应项(React 获取DOM中自定义属性)
点击删除按钮,删除列表中对应项本来是React比较基础的应用,可是应用情况变得复杂了以后,我还真想了一会儿才搞定. 简化一下应用场景:点击新增按钮,增加一条输入框,点击输入框旁边的按钮,删除该输入框( ...
随机推荐
- HTML学习笔记——常用元素及其属性(一)
1.img 标签 -- 代表HTML图像 img标签是单独出现的,<img /> 语法: <img src="URI" alt="alttext&quo ...
- Axis-Parallel Rectangle
D - Axis-Parallel Rectangle Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stat ...
- EasyNVR智能云终端硬件使用场景分析:如何实现软硬一体的视频上云整体解决方案
背景分析 在于众多的客户交流中,经常会被客户问到,"EasyNVR到底是软件还是硬件?"."EasyNVR能否出一个硬件的版本,摆脱自建服务器的压力?".&qu ...
- Introduction to Mathematical Thinking - Week 6 - Proofs with Quantifieers
Mthod of proof by cases 证明完所有的条件分支,然后得出结论. 证明任意 使用任意 注意,对于一个任意的东西,你不知道它的具体信息.比如对于任意正数,你不知道它是 1 还是 2等 ...
- 【转】hibernate中的映射文件xxx.hbm.xml详解总结
一.Hibernate映射文件的作用: Hibernate映射文件是Hibernate与数据库进行持久化的桥梁 二,Hibernate映射文件主要内容: (1).映射内容的定义: Hibern ...
- 使用Sentry集中化日志管理
在调试程序中,通过日志分期来排查BUG是一个重要手段,它可以说是程序调试的利器. 关于日志管理 随着应用组件变多,那么各coder对输出日志五花八门,有写入stdout,有写stderr, 有写到sy ...
- python多线程(二)
开启线程的两种方式 #方式一from threading import Threadimport timedef sayhi(name): time.sleep(2) print('%s sa ...
- COM 组件创建实例失败,原因是出现以下错误: c001f011 (Microsoft.SqlServer.ManagedDTS)
从 IClassFactory 为 CLSID 为 {AA40D1D6-CAEF-4A56-B9BB-D0D3DC976BA2} 的 COM 组件创建实例失败,原因是出现以下错误: c001f011. ...
- 000-mysql小技巧
1.使用Navicat 链接5.7版本出现 mysql 5.7.9 [Err] 1055报错解决,[Err] 1055 – Expression #1 of ORDER BY clause is no ...
- linux list.h 移植
Linux内核中List链表的实现,对于想进阶的程序员来说,无疑是一个很好的学习机会.内核实现了一个功能十分强大的链表,而且是开源的,用在其他需要的地方岂不是很省事. 一.看List实现前,先补充ty ...