getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement for componentWillReceiveProps. It is invoked after a component is instantiated as well as when it receives new props. It should return an object to update state, or null to indicate that the new props do not require any state updates.

import React, { Component, Fragment } from "react";

class FetchJson extends Component {
state = {
url: null,
data: null,
isLoading: true
}; static getDerivedStateFromProps(nextProps, prevState) {
console.log("getDerivedStateFromProps", nextProps);
if (prevState.url !== nextProps.url) {
return {
url: nextProps.url,
data: null,
isLoading: true
};
} return null;
} fetchAndUpdate = async () => {
const url = this.state.url;
const response = await fetch(url);
const result = await response.json();
this.setState(state => {
return { data: result, isLoading: false };
});
}; componentDidMount() {
this.fetchAndUpdate();
} componentDidUpdate() {
if (this.state.isLoading) {
this.fetchAndUpdate();
}
} render() {
return <Fragment>{this.props.render(this.state)}</Fragment>;
}
} export default FetchJson;

[React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3的更多相关文章

  1. React & update state with props & Object.assign

    React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...

  2. [React] Update State in React with Ramda's Evolve

    In this lesson we'll take a stateful React component and look at how we can refactor our setState ca ...

  3. React 深入系列3:Props 和 State

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...

  4. react 中state与props

    react 中state与props 1.state与props props是只读属性,只有在组件被实例化的时候可以赋值,之后的任何时候都无法改变该值.如果试图修改该值时,控制台会报错 only re ...

  5. react设置默认state和默认props

    1.默认状态设置 1.constructor (ES6) constructor(props) { this.state = { n: ... } } 2.getInitialState (ES5) ...

  6. React中state与props介绍与比较

    一.state 1.state的作用 state是React中组件的一个对象.React把用户界面当做是状态机,想象它有不同的状态然后渲染这些状态,可以轻松让用户界面与数据保持一致. React中,更 ...

  7. react --- React中state和props分别是什么?

    props React的核心思想就是组件化思想,页面会被切分成一些独立的.可复用的组件. 组件从概念上看就是一个函数,可以接受一个参数作为输入值,这个参数就是props,所以可以把props理解为从外 ...

  8. [React] Update Component State in React With Ramda Lenses

    In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...

  9. React中state和props分别是什么?

    整理一下React中关于state和props的知识点. 在任何应用中,数据都是必不可少的.我们需要直接的改变页面上一块的区域来使得视图的刷新,或者间接地改变其他地方的数据.React的数据是自顶向下 ...

随机推荐

  1. 昂贵的聘礼(Dijkstra)

    http://poj.org/problem?id=1062 每个物品看成一个节点,酋长的允诺也看作一个物品, 如果一个物品加上金币可以交换另一个物品,则这两个节点之间有边,权值为金币数,求第一个节点 ...

  2. 2205 等差数列(dp)

    2205 等差数列  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 钻石 Diamond     题目描述 Description 等差数列的定义是一个数列S,它满足了(S[i] ...

  3. oracle 创建命令

    环境变量设置(在Sqlplus中执行) create or replace directory filepath as 'D:\ORACLEBACKUP'; 备份脚本:expdp system/123 ...

  4. 从flink-example分析flink组件(1)WordCount batch实战及源码分析

    上一章<windows下flink示例程序的执行> 简单介绍了一下flink在windows下如何通过flink-webui运行已经打包完成的示例程序(jar),那么我们为什么要使用fli ...

  5. python 3:str.upper()与str.lower()(使字符串字母全部大写或小写)

    name = "Hello,World! Hello,Python!" print(name.upper()) #字母全部大写 print(name.lower()) #字母全部小 ...

  6. Scala学习1————scala开发环境搭建(windows 10)

    Scala开发环境搭建 先讲几点我学习scala的目的或者原因吧: JVM在企业中的霸主地位,Scala也是JVM上的语言,很有可能未来会从Java过度到Scala也不是不可能. 先进的函数式编程和面 ...

  7. yaml标记语言的简介

    今天遇到yml这个文件,挺懵的.也是百度了一把. 这篇博文不错:http://www.ibm.com/developerworks/cn/xml/x-1103linrr/ 这图画得不错:http:// ...

  8. easyui验证提示框 卡在屏幕上!!

    场景:验证提示框,关闭diglog窗口后 还显示在页面中 解决方法: 在窗口关闭事件中,删除提示框(这貌似并不可行),只能将验证提示框隐藏起来. $('#dialog').dialog({ onClo ...

  9. windows服务安装错误 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机

    今天安装windows服务的时候先是在本地安装测试通过,但是一到服务器就一直安装失败 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机 然 ...

  10. [转]浏览器缓存详解: expires, cache-control, last-modified, etag详细说明

    最近在对CDN进行优化,对浏览器缓存深入研究了一下,记录一下,方便后来者 画了一个草图: 每个状态的详细说明如下: 1.Last-Modified 在浏览器第一次请求某一个URL时,服务器端的返回状态 ...