The componentWillReceiveProps() method is being deprecated in future version of React (17). Many of us use this method day-to-day to check for incoming prop changes, store state, and to invoke side effects like logging or fetching data from a server.

In this lesson, we'll look at how to refactor an existing component that uses componentWillReceiveProps() to instead use getDerivedStateFromProps() and componentDidUpdate().

Additional Resources: https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#migrating-from-legacy-lifecycles

In short,

componentWillReceiveProps:

The new static getDerivedStateFromProps lifecycle is invoked after a component is instantiated as well as when it receives new props. It can return an object to update state, or null to indicate that the new props do not require any state updates.

should handle any local data changes:

  static getDerivedStateFromProps(nextProps, prevState) {
const { number } = nextProps; return number === prevState.number
? { computedMessage: "Same number, try again!", number }
: { computedMessage: null, number };
}

componentDidUpdate:

hanlde any async update

  componentDidUpdate(nextProps) {
const { number } = nextProps;
if (this.state.computedMessage === null) {
fakeServerRequest(this.props.number).then(result => {
this.setState({ computedMessage: result });
});
}
}

componentWillReceiveProps together with componentDidUpdate, this new lifecycle should cover all use cases for the legacy componentWillReceiveProps.

[React] Refactor componentWillReceiveProps() to getDerivedStateFromProps() in React 16.3的更多相关文章

  1. [React] Refactor a Class Component with React hooks to a Function

    We have a render prop based class component that allows us to make a GraphQL request with a given qu ...

  2. React系列(一):React入门

    React简介 1.由来 React是有Facebook开发出来用于构建前端界面的JS组件库,由于其背后的强大背景,使得这款库在技术开发上完全没有问题. 2.React的优势 解决大规模项目开发中数据 ...

  3. 如何使用TDD和React Testing Library构建健壮的React应用程序

    如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...

  4. react的类型检查PropTypes自React v15.5起已弃用,请使用prop-types

    最近使用React的类型检查PropTypes时,遇到错误:TypeError: Cannot read property 'array' of undefined 看了下自己的React版本:    ...

  5. react 也就这么回事 01 —— React 元素的创建和渲染

    React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...

  6. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...

  7. React Native是一套使用 React 构建 Native app 的编程框架

    React Native是一套使用 React 构建 Native app 的编程框架 React Native at first sight what is React Native? 跟据官方的描 ...

  8. react.js 从零开始(七)React (虚拟)DOM

    React 元素 React 中最主要的类型就是 ReactElement.它有四个属性:type,props,key 和ref.它没有方法,并且原型上什么都没有. 可以通过 React.create ...

  9. React环境配置(第一个React项目)

    使用Webpack构建React项目 1. 使用NPM配置React环境 NPM及React安装自行百度 首先创建一个文件夹,the_first_React 进入到创建好的目录,npm init,然后 ...

随机推荐

  1. LeetCode.5-最长回文子串(Longest Palindromic Substring)

    这是悦乐书的第342次更新,第366篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第3题(顺位题号是5).给定一个字符串s,找到s中最长的回文子字符串. 您可以假设s ...

  2. Python 40 数据库-约束

    约束 1.什么是约束 ? 除了数据类型以外额外添加的约束 2.为什么要使用约束 ? 为了保证数据的合法性 完整性 分类: 1.not null 非空约束,数据不能为空----------------- ...

  3. iTex导出PDF

    iText导出PDF,所需jar包如下: itext-asian-5.2.0.jar 支持导出中文的jar包 itextpdf-5.5.9.jar PDF核心jar包 bcprov-jdk15on-1 ...

  4. Laravel5.1学习笔记8 Blade模板

    简介 模板继承 定义一个页面布局模板 扩展一个页面布局模板 展示数据 控制语法的结构 Service Injection 扩展 Blade   简介 Blade 是 Laravel 提供的一个既简单又 ...

  5. css3中的box-sizing属性的使用

    box-sizing属性用来定义元素的width和height所表示的区域,该属性一般有三种值:content-box.border-box.inherit. 其中inherit表示box-sizin ...

  6. css的外边距合并或者外边距塌陷问题

    第一种情况: 已知两个宽和高均为100px,margin均为20px的div垂直排列,现象如下图所示: 当设置css1的margin-bottom:40px:或者css2的margin-top:40p ...

  7. What is the difference between PKCS#5 padding and PKCS#7 padding

    The difference between the PKCS#5 and PKCS#7 padding mechanisms is the block size; PKCS#5 padding is ...

  8. 记Python学习

    上周学的Python,感觉有点忘了,现在回顾一下... 一.Python安装及测试:https://www.cnblogs.com/weven/p/7252917.html 例子: Python自带的 ...

  9. python 直接存入Excel表格

    def write_excels(self, document): outwb = openpyxl.Workbook() outws = outwb.create_sheet(index=0) fo ...

  10. Keras学习基础(2)

    目录: Keras的模块结构 数据预处理 模型 网络层 网络配置 Keras中的数据处理 文本预处理 序列预处理 图像预处理 Keras中的模型 Sequential顺序模型 Model模型[通用模型 ...