react-parent-child-lifecycle-order
react-parent-child-lifecycle-order
react parent child lifecycle order
live demo
https://codesandbox.io/s/react-parent-child-lifecycle-order-33qrr
https://react-parent-child-lifecycle-order.stackblitz.io
https://stackblitz.com/edit/react-parent-child-lifecycle-order
import React, { Component } from "react";
import log from "../utils/log";
class Child extends Component {
constructor() {
super();
this.state = {};
log(`child constructor`, 0);
}
// new API
// getDerivedStateFromProps() {
// log(`child getDerivedStateFromProps`, 11);
// }
// getSnapshotBeforeUpdate() {
// log(`child getSnapshotBeforeUpdate`, 22);
// }
// old API
componentWillMount() {
log(`child WillMount`, 1);
}
// UNSAFE_componentWillMount() {
// log(`child WillMount`, 1);
// }
componentDidMount() {
log(`child DidMount`, 2);
}
componentWillReceiveProps() {
log(`child WillReceiveProps`, 3);
}
// UNSAFE_componentWillReceiveProps() {
// log(`child WillReceiveProps`, 3);
// }
shouldComponentUpdate() {
log(`child shouldComponentUpdate`, 4);
return true;
// return true or false;
}
componentWillUpdate() {
log(`child WillUpdate`, 5);
}
// UNSAFE_componentWillUpdate() {
// log(`child WillUpdate`, 5);
// }
componentDidUpdate() {
log(`child DidUpdate`, 6);
}
componentWillUnmount() {
log(`\nchild WillUnmount`, 7);
}
componentDidCatch(err) {
log(`child DidCatch`, err);
}
render() {
log(`child render`);
return (
<div className="child">
<h1>child-lifecycle-order</h1>
</div>
);
}
}
export default Child;
import React, { Component } from "react";
import Child from "./child";
import log from "../utils/log";
class Parent extends Component {
constructor() {
super();
this.state = {
show: true
};
// this.toggoleShow = this.toggoleShow.bind(this);
log(`parent constructor`, 0);
}
// new API
// getDerivedStateFromProps() {
// log(`child getDerivedStateFromProps`, 11);
// }
// getSnapshotBeforeUpdate() {
// log(`child getSnapshotBeforeUpdate`, 22);
// }
// old API
componentWillMount() {
log(`parent WillMount`, 1);
}
// UNSAFE_componentWillMount() {
// log(`parent UNSAFE_WillMount`, 1);
// }
componentDidMount() {
log(`parent DidMount`, 2);
}
componentWillReceiveProps() {
log(`parent WillReceiveProps`, 3);
}
// UNSAFE_componentWillReceiveProps() {
// log(`parent UNSAFE_WillReceiveProps`, 3);
// }
shouldComponentUpdate() {
log(`parent shouldComponentUpdate`, 4);
return true;
// return true or false;
}
componentWillUpdate() {
log(`parent WillUpdate`, 5);
}
// UNSAFE_componentWillUpdate() {
// log(`parent UNSAFE_WillUpdate`, 5);
// }
componentDidUpdate() {
log(`parent DidUpdate`, 6);
}
componentWillUnmount() {
log(`\n\nparent WillUnmount`, 7);
}
componentDidCatch(err) {
log(`parent DidCatch`, err);
}
// toggoleShow() {
// const { show } = this.state;
// this.setState({
// show: !show
// });
// }
toggoleShow = () => {
const { show } = this.state;
this.setState({
show: !show
});
};
render() {
log(`parent render`);
const { show } = this.state;
return (
<div className="parent">
<h1>parent-lifecycle-order</h1>
{/* <button onClick={this.toggoleShow.bind(this)}>toggoleShow</button> */}
{/* <button onClick={() => this.toggoleShow}>toggoleShow</button> */}
<button onClick={this.toggoleShow}>toggoleShow</button>
{show && <Child />}
</div>
);
}
}
export default Parent;
import React, { useState, useEffect } from "react";
import "./styles.css";
import Parent from "./components/parent";
export default function App() {
const [show, setShow] = useState(true);
const toggoleShow = () => {
setShow(!show);
};
useEffect(() => {
// Update the document title using the browser API
let flag = true;
if (flag) {
document.title = `react hooks ${show}`;
}
// cancel promise
return () => (flag = false);
}, [show]);
return (
<div className="App">
<h1>react-parent-child-lifecycle-order</h1>
<button onClick={toggoleShow}>toggoleShow</button>
{show && <Parent />}
</div>
);
}
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
react-parent-child-lifecycle-order的更多相关文章
- [NHibernate]Parent/Child
系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...
- React (Native) Rendering Lifecycle
How Does React Native Work? The idea of writing mobile applications in JavaScript feels a little odd ...
- 服务启动Apache服务,错误Parent: child process exited with status 3 -- Aborting.解决
不能启动apache,或者使用wamp等集成包后,唯独apache服务启动后有停止,但是把东西搬到其他机器上却没事问题可能和网络有关,我查了很多资料首先找打apache的错误报告日志,发现现实诸多的调 ...
- XPath学习:parent,child
XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...
- [React Fundamentals] Component Lifecycle - Updating
The React component lifecycle will allow you to update your components at runtime. This lesson will ...
- [React Fundamentals] Component Lifecycle - Mounting Usage
The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...
- [React Fundamentals] Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
- [React] React Fundamentals: Component Lifecycle - Updating
The React component lifecycle will allow you to update your components at runtime. This lesson will ...
- [React ] React Fundamentals: Component Lifecycle - Mounting Usage
The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...
- [React] React Fundamentals: Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
随机推荐
- get_or_create update_or_create
django/query.py at master · django/django https://github.com/django/django/blob/master/django/db/mod ...
- 正则re高级用法
search 需求:匹配出文章阅读的次数 #coding=utf-8 import re ret = re.search(r"\d+", "阅读次数为 9999" ...
- 算法总结篇---AC自动机
目录 写在前面 算法流程 引例: 概述: Trie树的构建(第一步) 失配指针(第二步) 构建失配指针 字典树和字典图 多模式匹配 例题 写在前面 鸣谢: OiWiki 「笔记」AC 自动机---Lu ...
- java打exe
参考文章: 注册码: https://www.cnblogs.com/jepson6669/p/9211208.html 官网: https://exe4j.apponic.com/ 在上篇基础上,将 ...
- JavaWeb——EL及JSTL学习总结
什么是EL表达式 为什么需要EL EL的主要作用 EL的语法 EL的开发步骤 EL实例练习 EL中的运算符 EL表达式显示内容的特点 EL的特点 EL隐式对象 EL隐式对象介绍 隐式对象实例练习 什么 ...
- EasyUI动态显示后台数据库中的数据
最近在完成一个项目,采用SSM框架搭建完成,前端使用EasyUI搭建页面: 其中涉及到一个查询显示功能:查询数据库中的数据,动态显示在页面之中,刚开始这部分十分有疑问,所以虚心向同学学习,现总结至博客 ...
- kubenetes 相关命令(转载)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xingwangc2014/article/details/51204224好久没写博客了,前段时间公 ...
- linux系统资源限制———ulimit命令
简介 Linux ulimit命令用于控制shell程序的资源. ulimit为shell内建指令,可用来控制shell执行程序的资源 推荐:https://blog.csdn.net/skiwnc/ ...
- (31)sed命令完全攻略
1.sed简介 Vim 采用的是交互式文本编辑模式,你可以用键盘命令来交互性地插入.删除或替换数据中的文本.但本节要讲的 sed 命令不同,它采用的是流编辑模式,最明显的特点是,在 sed 处理数据之 ...
- 国产App为什么如此“臃肿”?!
引言 App是Application的简称,正是因为有了丰富多彩的各类App,人们就可以通过它们来最大限度地发挥手中设备的功能.本文主要讨论手机上的App,因为手机的硬件和软件与十余年前相比早已有了巨 ...