背景:我在react文档里找生命周期的图,居然没有,不敢相信我是在推特上找到的。。。

正文

react v16.3 新生命周期:

  • static getDerivedStateFromProps
  • getSnapshotBeforeUpdata

1: getDerivedStateFromProps, 在render之前,返回一个obj更新state,或者null不更新state, 这个生命周期的使用场景是:state根据props变化。

 static getDerivedStateFromProps(nextProps: SearchProps, prevState: SearchState) {
const { location } = nextProps
const state = location.state
if (state && (state.keyword !== prevState.keyword)) {
const { keyword } = statereturn {
pageState: 0,
employee: {
hit: 0,
count: 0,
data: [],
currentOffset: 0
},
document: {
hit: 0,
count: 0,
data: [],
currentOffset: 0
},
keyword,
needExact: false
}
}
return null
}

2: getSnapshotBeforeUpdata  触发时间:在实际dom挂载之前,虚拟dom构建之后。返回的任何数据或者null,都将作为componentDidUpdate()的参数。

 getSnapshotBeforeUpdate(prevProps, prevState) {
// Are we adding new items to the list?
// Capture the scroll position so we can adjust scroll later.
if (prevProps.list.length < this.props.list.length) {
const list = this.listRef.current;
return list.scrollHeight - list.scrollTop;
}
return null;
} componentDidUpdate(prevProps, prevState, snapshot) {
// If we have a snapshot value, we've just added new items.
// Adjust scroll so these new items don't push the old ones out of view.
// (snapshot here is the value returned from getSnapshotBeforeUpdate)
if (snapshot !== null) {
const list = this.listRef.current;
list.scrollTop = list.scrollHeight - snapshot;
}
}

建议用法总结请移步:https://juejin.im/post/5aca20c96fb9a028d700e1ce

这里拷贝一个我用到的一种情况:props更新时重新请求

// old
componentWillReceiveProps(nextProps) {
if (nextProps.id !== this.props.id) {
this.setState({externalData: null});
this._loadAsyncData(nextProps.id);
}
} // new
static getDerivedStateFromProps(nextProps, prevState) {
// Store prevId in state so we can compare when props change.
if (nextProps.id !== prevState.prevId) {
return {
externalData: null,
prevId: nextProps.id,
};
}
// No state update necessary
return null;
}
componentDidUpdate(prevProps, prevState) {
if (this.state.externalData === null) {
this._loadAsyncData(this.props.id);
}
}

我的使用:

  componentDidUpdate(prevProps: SearchProps, prevState: SearchState) {
const { keyword, needExact } = this.state
if (keyword && (keyword !== prevState.keyword)) { // 上一次的state和这一次的对比,如果需要,就进行fetch数据,并setstate。其中this.state就是getDerivedStateFromProps返回的 更新后的state
if (needExact) {
this._fetchExactData(this.state.keyword)
this.setState({
needExact: false
})
} else {
Promise.all([this._fetchInitData('document'), this._fetchInitData('employee')])
.then(values => {
let pageState = 0
const { state } = this.props.location
if (state && state.type) {
pageState = state.type
} else if (!values[1] && values[0]) {
pageState = 1
}
this.setState({
pageState,
})
})
}
}
}

最后,记录一个小知识点:

由于state变化,需要出发请求数据的,在componentDidUpdate中进行。

a dive in react lifecycle的更多相关文章

  1. React Lifecycle

    React Lifecycle 分为三种: 初始化阶段 状态的更新 销毁 实例化: ReactDom.render 用于将模板转换成HTML语言,并插入DOM节点. 1.getDefaultProps ...

  2. React LifeCycle API

    React LifeCycle API old API & new API 不可以混用 demo https://codesandbox.io/s/react-parent-child-lif ...

  3. React LifeCycle Methods & re-learning 2019

    React LifeCycle Methods & re-learning 2019 v16.9.0 https://reactjs.org/docs/react-component.html ...

  4. 一图解析 React组件生命周期 (React Component Lifecycle)

     React LifeCycle v1 参考官方文档作成 可放大  参考:https://reactjs.org/docs/react-component.html 数字补丁数字补丁数字补丁数字补丁数 ...

  5. React (Native) Rendering Lifecycle

    How Does React Native Work? The idea of writing mobile applications in JavaScript feels a little odd ...

  6. react与jQuery对比,有空的时候再翻译一下

    参考资料:http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-t ...

  7. 如何用 React Native 创建一个iOS APP?(三)

    前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...

  8. 从性能角度看react组件拆分的重要性

    React是一个UI层面的库,它采用虚拟DOM技术减少Javascript与真正DOM的交互,提升了前端性能:采用单向数据流机制,父组件通过props将数据传递给子组件,这样让数据流向一目了然.一旦组 ...

  9. 携程React Native实践

    React Native(下文简称 RN)开源已经一年多时间,国内各大互联网公司都在使用,携程也在今年 5 月份投入资源开始引入,并推广给多个业务团队使用,本文将会分享我们遇到的一些问题以及我们的优化 ...

随机推荐

  1. 逆水行舟 —— MyBatis

    第一轮总结性笔记 这是一个很漫长的过程,我买了套课程,将在日后记录学习笔记,取名为逆水行舟系列 MyBatis的基础 根据MyBatis的官方介绍: 整个测试项目结构如下:使用Maven架构项目 po ...

  2. puppet-type

    puppet语法-type Table of Contents Custom Source 基本技能要求 Types简介 Type-Documentation Type-Properties Type ...

  3. Mybatis框架七:三种方式整合Spring

    需要的jar包: 新建lib文件夹放入jar包,Build Path-->Add To Build Path之后: 原始Dao开发示例: src下:新建核心配置文件sqlMapConfig.xm ...

  4. Mybatis3.2不支持Ant通配符TypeAliasesPackage扫描的解决方案

    业务场景 业务场景:首先项目进行分布式拆分之后,按照模块再分为为api层和service层,web层. 其中订单业务的实体类放在com.muses.taoshop.item.entity,而用户相关的 ...

  5. SpringBoot集成netty实现客户端服务端交互和做一个简单的IM

    看了好几天的netty实战,慢慢摸索,虽然还没有摸着很多门道,但今天还是把之前想加入到项目里的 一些想法实现了,算是有点信心了吧(讲真netty对初学者还真的不是很友好......) 首先,当然是在S ...

  6. Intellij IDEA神器居然还有这些小技巧

    概述 Intellij IDEA真是越用越觉得它强大,它总是在我们写代码的时候,不时给我们来个小惊喜.出于对Intellij IDEA的喜爱,我决定写一个与其相关的专栏或者系列,把一些好用的Intel ...

  7. js动态创建表单数据

    var formData = new FormData(); formData.append("file",fileList[i]); formData.append(" ...

  8. 使用 DryIoc 替换 Abp 的 DI 框架

    一.背景 你说我 Castle Windsor 库用得好好的,为啥要大费周章的替换成 DryIoc 库呢?那就是性能,DryIoc 是一款优秀而且轻量级的 DI 框架,整个项目代码就两个文件,加起来代 ...

  9. ES6进阶之路

    1.说出至少5个ES6的新特性,并简述它们的作用. . let关键字,用于声明只在块级作用域起作用的变量. . const关键字,用于声明一个常量. . 结构赋值,一种新的变量赋值方式.常用于交换变量 ...

  10. vc-mysql-sniffer统计MySQL的SQL分布

    有时候我们需要统计线上的SQL执行情况,比如想知道哪条SQL执行最频繁,我们可以开启general_log,然后进行统计,但是general_log开启非常损耗性能,那么我们可以使用vc-mysql- ...