目录javascript
因为 React 的版本更新频繁,各种的新特性也是让人眼花缭乱的,为了方便本身查询最新的以及过往的 各个 React 版本 api、生命周期函数。 这里就用 caniuse 的方式作一个 方便查询的小功能。html
那么要实现这个小功能以前,咱们必需要对 React 的各类版本进行仔细的解读。 最快捷的方式就是 直接 经过官方文档来获取最真实的信息。
React 版本 各个阶段 API | Mounting(绑定) | Updating(数据更新) |
---|---|---|
V 16.0.0 | constructor() componentWillMount() render() componentDidMount() |
componentWillReceiveProps() shouldComponentUpdate() componentWillUpdate() render() componentDidUpdate() |
V 16.3.2 | constructor() static getDerivedStateFromProps() componentWillMount() / UNSAFE_componentWillMount() render() componentDidMount() |
componentWillReceiveProps() / UNSAFE_componentWillReceiveProps() static getDerivedStateFromProps() shouldComponentUpdate() componentWillUpdate() /UNSAFE_componentWillUpdate() render() getSnapshotBeforeUpdate() componentDidUpdate() |
V 16.5.2 | constructor() static getDerivedStateFromProps() render() componentDidMount() |
static getDerivedStateFromProps() shouldComponentUpdate() render() getSnapshotBeforeUpdate() componentDidUpdate() |
V 16.7.0(最新) | constructor() static getDerivedStateFromProps() render() componentDidMount() |
static getDerivedStateFromProps() shouldComponentUpdate() render() getSnapshotBeforeUpdate() componentDidUpdate() |
1-一、About
Components
java
一、Components let you split the UI into independent, reusable pieces, and think about each piece in isolation 组件容许您将UI拆分为独立的、可重用的部分,并独立地考虑每一个部分
二、Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen. 从概念上讲,组件相似于JavaScript函数。它们接受任意输入(称为“props”),并返回描述屏幕上应该出现的内容的React元素。
1-二、Component's presentation (展示形式)react
The simplest way to define a component is to write a JavaScript function:
(最简单的方式)git
function Welcome(props) { return <h1>Hello, {props.name}</h1>; }
You can also use an ES6 class to define a component:
(你也可使用 ES2015 中 类 的方式)github
class Welcome extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; } }
上面的二种写法,目前来看是
等价的
.ajax
任何都 React 版本,关于 Lifecycle 咱们均可以找到对应的几个状态,来进行不一样的 api 的差别的对比。这样也是方便,咱们进行记忆的。api
官方文档传送门:
React V 16.0.0 官方文档浏览器
constructor()
服务器
例如:
constructor(props) { super(props); this.state = { color: props.initialColor }; }
componentWillMount()
render()
如下类型:
React elements
、String and numbers
、 Portals
、null
当 render null、 false、ReactDOM.findDOMNode(this) 的时候会返回 null
componentDidMount()
componentWillReceiveProps()
componentWillReceiveProps(nextProps)
shouldComponentUpdate()
shouldComponentUpdate(nextProps, nextState)
componentWillUpdate()
componentWillUpdate(nextProps, nextState)
componentDidUpdate()
componentDidUpdate(prevProps, prevState)
componentWillUnmount()
componentDidCatch()
componentDidCatch(error, info)
错误边界是响应组件,这些组件捕捉子组件树中的任何位置的JavaScript错误,记录这些错误,并显示一个回退UI,而不是崩溃的组件树。错误边界在呈现、生命周期方法和下面整个树的构造函数中捕获错误。
若是类组件定义了这个生命周期方法,它就会成为一个错误边界。在其中调用setState()容许您在下面的树中捕获未处理的JavaScript错误并显示回退UI。只使用错误边界从意外异常中恢复;不要试图用它们来控制流程。
可能就会有同窗问了,为啥 第二部分的内容不讲了?
答: 这真的没什么好讲的。
以上则是 React V16.0.0 的所有内容,欢迎你们一块儿讨论~后面还有 关于剩下版本的 apis 变化的介绍,
主要是以 为何 react 开发组要 干掉这些 api 以及 新的 api 能解决什么问题为出发点。介绍 ReactJS 这些年的进化
帮助你们一块儿来走进这个框架。
GitHub
地址:(欢迎 star 、欢迎推荐 : )