[React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3
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的更多相关文章
- React & update state with props & Object.assign
React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...
- [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 ...
- React 深入系列3:Props 和 State
文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...
- react 中state与props
react 中state与props 1.state与props props是只读属性,只有在组件被实例化的时候可以赋值,之后的任何时候都无法改变该值.如果试图修改该值时,控制台会报错 only re ...
- react设置默认state和默认props
1.默认状态设置 1.constructor (ES6) constructor(props) { this.state = { n: ... } } 2.getInitialState (ES5) ...
- React中state与props介绍与比较
一.state 1.state的作用 state是React中组件的一个对象.React把用户界面当做是状态机,想象它有不同的状态然后渲染这些状态,可以轻松让用户界面与数据保持一致. React中,更 ...
- react --- React中state和props分别是什么?
props React的核心思想就是组件化思想,页面会被切分成一些独立的.可复用的组件. 组件从概念上看就是一个函数,可以接受一个参数作为输入值,这个参数就是props,所以可以把props理解为从外 ...
- [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. ...
- React中state和props分别是什么?
整理一下React中关于state和props的知识点. 在任何应用中,数据都是必不可少的.我们需要直接的改变页面上一块的区域来使得视图的刷新,或者间接地改变其他地方的数据.React的数据是自顶向下 ...
随机推荐
- yrzl-cloud
- Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element
题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in ...
- linux的shell函数参数
在Shell中,调用函数时可以向其传递参数.在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数... 带参数的函数示例: #!/bin/bash funWit ...
- DIV+CSS设计时浏览器兼容性
近期用Div+css做了个企业网站,在浏览器中测试的时候确发现在IE7中显示正常的页面,在ie6中非常混乱,当时第一感觉就想到了兼容问题,可是百思不得其解应该从哪下手,经过一两天的查资料, ...
- cocos creator制作微信小游戏
2019-05-30 22:11:47 基础: javaScript基础 https://www.bilibili.com/video/av34087791?from=search&sei ...
- react中withRouter解决props返回为空
利用 react + antd 框架书写导航栏时,遇到了几个坑,分别是一级菜单和二级菜单在点击的情况下,高亮没有任何问题,但是再点击浏览器返回按钮时,却就乱套了. 1. 二级菜单中,我们可以通过 pr ...
- MyEclipse创建SSH项目(Java web由maven管理)
JavaEE后台开发,MyEclipse创建SSH项目,MyEclipse创建Java web 由maven管理的SSH项目. Demo工程源码github地址 1.创建SSH项目 1.创建web工程 ...
- NuGet 下载dll
PM> Install-Package DotNetCore.CAP PM> Install-Package xxxdll
- SQL server基本语法
此处源于一个基本的SQL Server试题,基本上涵盖了SQL Server的全部基本语法,粘贴在此处,权当分享 --1. 创建TestDB数据库 create database TestDB; ...
- 折纸---珠穆朗玛问题----简单for 循环
一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)? package com.zuoye.test; public class Zhezhi { public s ...