严格模式可以想成是React的debug工具,严格模式不会渲染出任何的UI元件,它会检查其包覆下所有的Component和底下的子元素是否有合乎规则。
import React from 'react';
function ExampleApplication() {
return (
<div>
<Header />
<React.StrictMode>
<div>
<ComponentOne />
<ComponentTwo />
</div>
</React.StrictMode>
<Footer />
</div>
);
}
上述范例中Header和Footer这两个Component都不会被检查到,唯有ComponentOne、ComponentTwo和底下的子元素会被检查到。
严格模式将会检测以下几点:
某些遗留的生命周期在非同步的React里使用是不安全的,使用严格模式的话,在浏览器渲染react产生画面的时候,可以在console中找到警告提示文字。
由於legacy string ref API含有诸多缺点,故使用严格模式时会提出警告。官方建议使用callback form取代string ref。
findDOMNode是一种搜寻Component里面DOM节点的方法,只能用在回传一个单一且永远不改变的 DOM 节点;但react後来有提供ref的方式,ref可以直接设在指定的DOM节点上,解决了findDOMNode的限制,故react较推荐使用ref。
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.wrapper = React.createRef();
}
render() {
return <div ref={this.wrapper}>{this.props.children}</div>;
}
}
React会在下列两种情况运作:
>>: Day20:SwiftUI—GeometryReader
六个思考帽是着名学者爱德华·德·波诺(Edward de Bono)提出的思考工具,代表了六个不同的...
despite all the benefits we stated yesterday, toa...
CISSP (Certified Information Systems Security Pro...
前言 网路通讯协定就是为电脑进行资料交换而建立的规章或标准的集合。常用的有TCP/IP协定、HTTP...
-重要性分析和业务影响分析 业务影响分析(BIA)是业务连续性管理的关键过程。它分析了中断对关键活...