React components render order All In One

components render order / components lifecycle

DOM tree render order

React diff

React fiber

当父组件进行重新渲染操作时,即使子组件的props或state没有做出任何改变,也会同样进行重新渲染

当子组件进行重新渲染操作时,只有子组件会同样进行重新渲染

parent component change props

import Parent from "./components/parent";

export default function App() {
const [name, setName] = useState(`xgqfrms`);
const [show, setShow] = useState(true);
const toggleShow = () => {
setShow(!show);
};
const updateName = () => {
setName(`webfullstack`);
};
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={toggleShow}>toggleShow</button>
<p>update props name</p>
<button onClick={updateName}>updateName</button>
{show && <Parent name={name}/>}
</div>
);
}

parent component change state

import React, { Component } from "react";

import Child from "./child";
import log from "../utils/log"; class Parent extends Component {
constructor(props) {
super();
this.state = {
show: true,
randomName: props.name,// "xgqfrms"
};
this.toggleShow = this.toggleShow.bind(this);
log(`parent constructor`, 0);
}
getRandomName = () => {
const randomName = Math.random() > 0.5 ? `abc` : `xyz`;
this.setState({
randomName,
});
};
toggleShow = () => {
const { show } = this.state;
this.setState({
show: !show
});
};
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);
}
render() {
log(`parent render`);
const {
show,
randomName,
} = this.state;
return (
<div className="parent">
<h1>parent-lifecycle-order</h1>
<button onClick={this.getRandomName}>get random name</button>
<p>randomName = {randomName}</p>
<button onClick={this.toggleShow}>toggleShow</button>
{show && <Child />}
</div>
);
}
} export default Parent;

child component change state



demos

https://codesandbox.io/s/react-parent-child-lifecycle-order-update-parent-state-render-render-2559w





refs

https://stackoverflow.com/questions/44654982/in-which-order-are-parent-child-components-rendered

https://scotch.io/courses/getting-started-with-react/parent-child-component-communication



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


React components render order All In One的更多相关文章

  1. [React] Recompose: Theme React Components Live with Context

    SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. P ...

  2. [React] Styling React Components With Aphrodite

    Aphrodite is a library styling React components. You get all the benefits of inline styles (encapsul ...

  3. [React] Extracting Private React Components

    we leverage private components to break our render function into more manageable pieces without leak ...

  4. Rendering React components to the document body

    React一个比较好用的功能是其简单的API,一个组件可以简单到一个return了组件结构的render函数.除了一个简单的函数之外,我们还有了一段有用且可复用的代码片段. 问题 不过有时候可能会受到 ...

  5. React Components Template

    React Components Template "use strict"; /** * * @author xgqfrms * @license MIT * @copyrigh ...

  6. [React] Create and import React components with Markdown using MDXC

    In this lesson I demonstrate how to use the library MDXC to create and import React components with ...

  7. React Components之间的通信方式了解下

    先来几个术语: 官方 我的说法 对应代码 React element React元素 let element=<span>A爆了</span> Component 组件 cla ...

  8. [Poi] Use Markdown as React Components by Adding a Webpack Loader to Poi

    Poi ships with many webpack loaders included, but you may run into scenarios where you'll need to cu ...

  9. [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

    Rather than using Components to push streams into other Components, mapPropsStream allows you to cre ...

随机推荐

  1. Mybatis SQL映射文件详解

    Mybatis SQL映射文件详解 mybatis除了有全局配置文件,还有映射文件,在映射文件中可以编写以下的顶级元素标签: cache – 该命名空间的缓存配置. cache-ref – 引用其它命 ...

  2. 关于redis搭建环境

    首先,window键+r 输入cmd进入dos命名窗口,我的redis是装在了d盘,so我得输入cd:或者d:进入d盘,cd\redis文件夹路径,这样的话,直接输入  redis-server -- ...

  3. PHPday01

    1:概念 1.1.1 静态网站和动态网站 静态网站:不支持数据交互的网站,(html,htm) 动态网站:支持数据交互的网站 实现动态网站的技术: 实现技术 网站后缀 ASP技术 .asp PHP . ...

  4. hadoop及NameNode和SecondaryNameNode工作机制

    hadoop及NameNode和SecondaryNameNode工作机制 1.hadoop组成 Common MapReduce Yarn HDFS (1)HDFS namenode:存放目录,最重 ...

  5. JVM 详解,大白话带你认识 JVM

    前言 如果在文中用词或者理解方面出现问题,欢迎指出.此文旨在提及而不深究,但会尽量效率地把知识点都抛出来 一.JVM的基本介绍 JVM 是 Java Virtual Machine 的缩写,它是一个虚 ...

  6. Python学习【第2篇】:基本数据类型

    基本数据类型 字符串 ---------n1 = "xiaoxing"   n2 = "admin"  n3 = "123"  n4 = & ...

  7. 五种C语言非数值计算的常用经典排序算法

    摘要:排序是计算机的一种操作方法,其目的是将一组"无序"的记录序列调整为"有序"的记录序列,主要分为内部排序和外部排序. 排序 排序是计算机的一种操作方法,其目 ...

  8. Spring Boot:添加导出Excel表格功能

    1.添加POI依赖 2.创建EXCEL实体类 3.创建表格工具类 4.创建ExcelConstant 5.创建ExcelController 1.添加POI依赖 <dependency> ...

  9. 基于efcore的分表组件开源

    ShardingCore ShardingCore 是一个支持efcore 2.x 3.x 5.x的一个对于数据库分表的一个简易扩展, 目前该库暂未支持分库(未来会支持),仅支持分表,该项目的理念是让 ...

  10. Pytest(6)重复运行用例pytest-repeat

    前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来. 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例, ...