reactjs - 为什么反应组件类的实例变量(不是状态变量)没有在构造函数中初始化值?
问题描述
我对 React Component 类变量(不是状态变量)有一个奇怪的问题。我在 Component 类中定义了一些变量。我在 shouldComponentUpdate() 函数中使用这个变量来决定组件是否应该更新。为简单起见,让我们说:
constructor(props) {
super(props);
this.variable = false;
console.log(this.variable);
}
shouldComponentUpdate(nextProps, nextState) {
console.log(this.variable);
if(this.variable === false) {
this.variable = true;
return true;
}
return false;
}
问题是:当我刷新我的网页时,constructor
首先被调用,然后我的shouldComponentUpdate()
函数被调用,但这里this.variable
碰巧是true
。为什么会发生这种情况?谁做this.variable
的true
?
PS:我正在使用开发环境:http://localhost:3000,如果它与此有任何关系。
解决方案
推荐阅读
- linux - elasticsearch 进程的最大文件描述符太低 - 至少增加到 [65536]
- java - 如何知道从哪个分支发出的 .jar nexus 工件
- elasticsearch - 如何从 ansible_results 解析 logstash /grok 中的 json
- javascript - 如果我正在访问儿童中包含的链接,如何显示和隐藏 ul
- html - 用 html 和 css 制作精细的 tictactoe 线条
- javascript - 如何在 Angular 7 中将初始值和验证设置为 ng 自动完成?
- html - 如何在网页背景中以随机形状合并三种不同颜色
- elasticsearch - 如何使用 Nest ElasticClient 基于 Url 域名执行术语聚合
- python - 是否有计算“罗马尼亚地图”问题启发式的公式?
- html - 全屏背景图像在 Firefox 上不起作用