严格模式(Reatc.StrictMode)

StrictMode是一个用以标记出应用中潜在问题的工具。就是Fragment,StrictMode不会渲染任何真是的UI。它为其后代元素触发额外的检查的警告

注:严格模式检查只在开发模式下运行,不会与生产模式冲突

可以在代码的任何地方启用严格模式。例如:

// 入口文件
React.render(<React.StrictMode><App /></React.StrictMode>,document.getElementById(“root")
)
// 单个组件中
import React from “react”;
function Home(){return (<div className=“home”><React.StrictMode><ComponentTable /><ComponentDialog /></React.StrictMode><CommonInfo /></div>)
}

上述案例中,在入口文件中使用,会对他的所有后代元素都进行检查;在单个组件中使用,会对CommpoentTable和CommonentDialog以及他们的所有后代元素进行检查,不会对CommonInfo组件运行严格模式

使用StrictMode的优点:

  • 识别不安全的声明周期组件
  • 有关旧式字符串ref用法的警告
  • 关于使用废弃的findDOMMode方法的警告
  • 检测意外的副作用
  • 检测过时的context API

生命周期警告

过时的组件生命周期往往会带来不安全的编码实践,具体函数如下:

  1. componentWillMount
  2. componentWillReceiveProps
  3. componentWillUpdate

16.3:为不安全的生命周期引入别名

  1. UNSAFE_componentWillMount
  2. UNSAFE_componentWillReceiveProps
  3. UNSAFE_componentWillUpdate
1、componentWillMount (旧的名称正常使用)
2、componentWillReceiveProps(旧的名称正常使用)
3、componentWillUpdate(旧的名称正常使用)

16.3<16.x<17.0

  1. UNSAFE_componentWillMount
  2. UNSAFE_componentWillReceiveProps
  3. UNSAFE_componentWillUpdate
1、componentWillMount (旧的名称在开发模式下会产生一个警告)
2、componentWillReceiveProps(旧的名称在开发模式下会产生一个警告)
3、componentWillUpdate(旧的名称在开发模式下会产生一个警告)

17.0 只有新的 “UNSAFE_” 生命周期名称可以使用

  1. UNSAFE_componentWillMount
  2. UNSAFE_componentWillReceiveProps
  3. UNSAFE_componentWillUpdate
1、componentWillMount (已经删除不能使用)
2、componentWillReceiveProps (已经删除不能使用)
3、componentWillUpdate (已经删除不能使用)

当启用严格模式时,React 会列出使用了不安全生命周期方法的所有 class 组件,并打印一条包含这些组件信息的警告消息,如下所示:

ref API 的警告

React 提供了两种方法管理 refs 的方式:已过时的字符串 ref API 的形式及回调函数 API 的形式。尽管字符串 ref API 在两者中使用更方便,但是它有一些缺点,因此官方推荐采用回调的方式。

你可能了解过之前的 API 中的 string 类型的 ref 属性,例如 “textInput”。你可以通过 this.refs.textInput 来访问 DOM 节点。

class MyComponent extends React.Component {render() {return <input type="text" ref="textInput" />;}componentDidMount() {this.refs.textInput.focus();}
}

React 16.3 新增了第三种选择,它提供了使用字符串 ref 的便利性,并且不存在任何缺点。

检测副作用

渲染阶段的生命周期包括以下 class 组件方法:

  1. constructor
  2. componentWillMount (or UNSAFE_componentWillMount)
  3. componentWillReceiveProps (or UNSAFE_componentWillReceiveProps)
  4. componentWillUpdate (or UNSAFE_componentWillUpdate)
  5. getDerivedStateFromProps shouldComponentUpdate render setState
    更新函数(第一个参数)

因为上述方法可能会被多次调用,所以不要在它们内部编写副作用相关的代码,这点非常重要。忽略此规则可能会导致各种问题的产生,包括内存泄漏和或出现无效的应用程序状态。不幸的是,这些问题很难被发现,因为它们通常具有非确定性。

严格模式不能自动检测到你的副作用,但它可以帮助你发现它们,使它们更具确定性。通过故意重复调用以下函数来实现的该操作:

  1. class 组件的 constructor,render 以及 shouldComponentUpdate 方法
  2. class组件的生命周期方法 getDerivedStateFromProps
  3. 函数组件体
  4. 状态更新函数 (即 setState 的第一个参数)
  5. 函数组件通过使用 useState,useMemo 或者 useReducer

context API 警告

过时的 context API 容易出错,将在未来的主要版本中删除。在所有 16.x 版本中它仍然有效,但在严格模式下,将显示以下警告:


旧版本的使用

import React, { Component } from 'react';
import PropTypes from 'prop-types';class App extends Component {static childContextTypes = {theme: PropTypes.string}getChildContext () {return {theme: 'dark'}}render() {return (<Toolbar />);}
}function Toolbar (props) {return (<div><ThemedButton /></div>)
}class ThemedButton extends Component {static contextTypes = {theme: PropTypes.string}render() {return (<button>{this.context.theme}</button>);}
}export default App;

新版本的使用

import React, { Component } from 'react';
const ThemeContext = React.createContext('light');class App extends Component {render() {return (<ThemeContext.Provider value="dark"><Toolbar /></ThemeContext.Provider>);}
}function Toolbar (props) {return (<div><ThemedButton /></div>)
}class ThemedButton extends Component {static contextType = ThemeContextrender() {return (<button>{this.context}</button>);}
}export default App;

React严格模式-React.StrictMode相关推荐

  1. [翻译]React组件模式

    原文地址:https://medium.com/teamsubchannel/react-component-patterns-e7fb75be7bb0 作者:William Whatley 摘要:本 ...

  2. 大前端快闪二:react开发模式 一键启动多个服务

    最近全权负责了一个前后端分离的web项目,前端使用create-react-app[1], 后端使用golang做的api服务. npx create-react-app my-app cd my-a ...

  3. react中的状态机_在基于状态图的状态机上使用React的模式

    react中的状态机 by Shawn McKay 肖恩·麦凯(Shawn McKay) 在基于状态图的状态机上使用React的模式 (Patterns for using React with St ...

  4. react开发模式_通过开发带有精灵动画的游戏来学习高级React模式

    react开发模式 by Pavel Vlasov 通过帕维尔·弗拉索夫(Pavel Vlasov) 通过开发带有精灵动画的游戏来学习高级React模式 (Learn advanced React p ...

  5. React 生产模式 Best Practice

    React 生产模式 Best Practice 一些用于生产环境的 Best Practice 检查 dependency 的安全性 socket dev,以 React 为例: kandi 禁用 ...

  6. 从零开始React:一档 React环境搭建,语法规则,基础使用

    手挽手带你学React入门第一期,带你熟悉React的语法规则,消除对JSX的恐惧感,由于现在开发中都是使用ES6语法开发React,所以这次也使用ES6的模式进行教学,如果大家对ES6不熟悉的话,先 ...

  7. [react] 写一个react的高阶组件并说明你对高阶组件的理解

    [react] 写一个react的高阶组件并说明你对高阶组件的理解 定义高阶组件 import React, { Component } from 'react';const simpleHoc = ...

  8. react hooks使用_何时使用React Suspense和React Hooks

    react hooks使用 React Suspense对Monad就像钩子对应用符号一样 (React Suspense is to a Monad as Hooks are to Applicat ...

  9. react hooks_为什么选择React Hooks,我们如何到达这里?

    react hooks by Ryan Yurkanin 瑞安·尤卡宁(Ryan Yurkanin) 为什么选择React Hooks,我们如何到达这里? (Why React Hooks, and ...

最新文章

  1. OC系列foundation Kit基础-NSDate
  2. 通信原理-确知信号的最佳接收
  3. 【js】vue 2.5.1 源码学习(二) 策略合并
  4. bert 中文 代码 谷歌_如何用最强模型BERT做NLP迁移学习?
  5. 趣味数据故事_坏数据的好故事
  6. 计算机系统集成难点,企业MES实施中存在的难点及建议
  7. Entity Framework 4 in Action 读书笔记——开篇
  8. Ubuntu 装机必备设置与软件安装
  9. QDomNode读取xml
  10. UVA621 Secret Research【水题】
  11. 前轮转向最大角度设计原来_五桥转向故障
  12. Java23个设计模式的简明教程
  13. linux usb有线网卡驱动_Linux系统安装R8169网卡驱动的方法
  14. 单片机内存及运行原理
  15. BTA前瞻 | CyberMiles创始人卢亮:愿拿百万年薪求区块链人才
  16. ES term terms 查询
  17. html怎么把图片做成抖动效果,css简单实现图片logo抖动摇晃效果
  18. python如何连redis_Python连接Redis的基本配置方法
  19. android按键模拟测试
  20. 计算机软考access 题的做法_计算机二级Access 操作题汇总

热门文章

  1. DAEMON Tools Pro 试用期破解
  2. php ffmpeg截图,PHP调用ffmpeg对视频截图并拼接脚本
  3. 美图秀秀DBA谈MySQL运维及优化
  4. 关于codewarrior 安装到64位的win7系统
  5. Linux mode命令,linux命令
  6. 高考平行报志愿计算机录取规则,2018天津高考平行志愿录取规则
  7. PS另存为web格式,找不到路径
  8. nginx限流、配置管理、重定向、防盗链
  9. 艾薇魔盒 v1.0.1
  10. Myeclipse 主题下载