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.

最新文章

  1. C#调用Windows API函数截图
  2. php/js互传cookie中文乱码的问题
  3. html5新特性之音频、视频
  4. SSE技术详解:一种全新的HTML5服务器推送事件技术
  5. maven 热部署成功案列
  6. [Express] Level 2: Middleware -- 2
  7. ASP.NET MVC 4 如何避免数据库被自动创建或自动迁移
  8. Oracle中存储过程传入表名学习
  9. “文件XXX正由另一进程使用,因此该进程无法访问此文件”
  10. WeUI
  11. ICON图标文件解析
  12. LCA 学习算法 (最近的共同祖先)poj 1330
  13. 我从现象中学到的CSS
  14. js常用内置对象及方法
  15. Linux ext2文件系统之初步思考
  16. Mysql根据一个基库生成其他库与其不同的库升级脚本
  17. javascript之DOM编程设置节点插入节点
  18. Windows下用python来获取微信撤回消息
  19. 【LDA】周志华
  20. Eloquent JavaScript #06# class

热门文章

  1. Python入门 来点栗子
  2. Squares(枚举+set 查找)
  3. 【Luogu4389】付公主的背包
  4. MySQL-基础操作之增删改查
  5. Elasticserach 同步索引报错:ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]
  6. ubuntu Ngin Install
  7. git上
  8. 使用NDK编译VTK
  9. <转>python 发送邮件实例
  10. 记录:Ubuntu下升级Python从2.x到3.x