reactjs - 挂载上的 ReactJS 方法
问题描述
最近我进行了一次在线测试,其中提出了以下问题:
挂载 React 组件时不执行以下哪个方法?
- 构造函数()
- 使成为()
- 组件WillReceiveProps()
- 组件DidMount()
我相信所有这些方法都会在安装 React 组件时执行。但是我确实选择了选项 4.componentDidMount(),即使它是错误的,因为我必须选择一个选项,是的,在线测试说它是错误的。 根据 Pluralsight 上的一篇文章,上述所有方法都将在挂载时执行(不推荐使用 componentWillReceiveProps(),应使用静态 getDerivedStateFromProps())。
我不明白的是
- 问错了吗?
- 选项错了吗?
- 我对问题的理解是错误的?
我很困惑。请帮助我获得正确的观点。
提前致谢。
解决方案
The documentation for componentWillReceiveProps says
is invoked before a mounted component receives new props.
It operates on a already mounted component receiving new props. A component being mounted cannot receive new props until it has initial props.
Also note that getDerivedStateFromProps
is not a 1 for 1 replacement method. It is suggested as an alternative for operations that were typically done in componentWillReceiveProps
, but they do not do the same thing.
推荐阅读
- jestjs - 使用 vue-test 和 Jest 测试 b-form-radio
- java - Java Mission Control (JMC) 无法更改 MBean 属性
- google-apps-script - 停止脚本创建 PDF 副本
- algorithm - 不了解具体功能
- java - libGDX - Box2D 世界步骤中的无限循环
- r - 如何在ggtree中按组为树的提示着色?
- flutter - 如何在异步功能后更改 UI?
- php - PHP 单元测试因发布请求而失败
- php - Symfony 4 中 CollectionType 表单的问题
- d3.js - 自定义时间间隔无限循环