- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我构建了一个简单的计数器应用程序:
class Counter extends React.Component {
constructor(props) {
super(props);
this.handleAddOne = this.handleAddOne.bind(this);
this.handleMinusOne = this.handleMinusOne.bind(this);
this.handleReset = this.handleReset.bind(this);
this.state = {
count: 0
};
}
componentDidMount() {
const stringCount = localStorage.getItem('count');
const count = parseInt(stringCount);
if (isNaN(count) === false) {
this.setState(() => ({ count }));
}
}
componentDidUpdate(prevProps, prevState) {
if (prevState.count !== this.state.count) {
localStorage.setItem('count', this.state.count);
console.log('componentDidUpdate');
}
}
handleAddOne() {
this.setState((prevState) => {
return {
count: prevState.count + 1
}
});
}
handleMinusOne() {
console.log('handleMinusOne');
this.setState((prevState) => {
return {
count: prevState.count - 1
}
});
}
handleReset() {
this.setState(() => {
return {
count: 0
}
});
}
render() {
return (
<div>
<h1>Count: {this.state.count}</h1>
<button onClick={this.handleAddOne}>+</button>
<button onClick={this.handleMinusOne}>-1</button>
<button onClick={this.handleReset}>reset</button>
</div>
);
}
}
ReactDOM.render(<Counter />, document.getElementById('app'));
我的问题是关于componentDidUpdate()
。在其中,我检查 prevState.count
是否与 this.state.count
不同。如果不相同,则将 localStorage
设置为新计数。如果相同,我什么也不做。
在当前的 componentDidUpdate() 中,我需要 prevProps
作为该函数正常工作的参数。例如,如果我只有这个:
componentDidUpdate(prevState) {
if (prevState.count !== this.state.count) {
localStorage.setItem('count', this.state.count);
console.log('componentDidUpdate');
}
}
然后,每次重复按下重置按钮时,组件都会设置 localStorage
,即使计数仍为 0。
这是怎么回事?如果我从未在该函数中使用过 props
,为什么我需要 prevProps
才能让 componentDidUpdate()
正常工作?
最佳答案
componentDidUpdate
中的第一个参数是 prevProps
。第二个参数是 prevState
。 The documentation明确指出:
componentDidUpdate(prevProps, prevState, snapshot)
这个
componentDidUpdate(prevState) {...}
不是钩子(Hook)的正确签名。尽管第一个参数名为 prevState
,但它包含以前的 props。
可以根据函数参数的数量来替换函数参数,但这在 React 中没有实现,并且通常被认为是一种不好的做法,因为这会导致更复杂的签名。
为了不引起 linter 警告,未使用的参数可以按照约定加下划线:
componentDidUpdate(_prevProps, prevState) {...}
关于javascript - 为什么我需要在 componentDidUpdate() 中使用 prevProps 和 prevState ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53734007/
我正在尝试重构代码库,但我陷入了困境。我基本上是尝试根据选择框的 onChange 事件更新状态。 在本例中,handleFilterChange 函数中的 searchCriteria 参数是 in
这是场景。我有一个父组件和一个子组件。父组件执行 ajax 调用并将数据推送到子组件。 当子组件接收到新数据时,在(子组件)的 componentDidUpdate 中,我将重新设置其内部状态。 co
我正在测试一些代码,以尝试理解有关使用 setState() 的竞争条件。 我的代码可以找到here 我的代码如下: import React from "react"; export default
我的数组状态正在渲染,我可以毫无问题地推送新元素,但是,当我尝试添加特定索引时, react 正在改变先前元素的状态。先前状态的示例: {this.state.numOfRows.map((v, k)
我正在观看有关使用 React 的视频教程。演示者当前正在详细介绍如何向 UI 添加切换按钮。他们说先尝试一下,然后看看他们是如何做的,所以我自己实现了。我的实现与他们的有点不同,只是处理程序不同;但
下面是一个简单的 React 计数器。但我有3个问题。 第 3 行的状态是什么?它看起来像一个全局变量,如果它前面有 var 或 const 就有意义。这是生命周期函数/var吗? 我必须从 Reac
下面我的 react 方法有什么问题 toggleReply = () => { this.setState(prevState => ({ reply: !prevState.rep
我试图更新存储在数组中的对象。但得到 TypeError: prevState.blockHash 是不可迭代的。这是我的构造函数 constructor(props) { super(props
这个问题已经有答案了: When to use functional setState (3 个回答) When to use React setState callback (6 个回答) What
我有处于类状态的对象。 man: { name: "John", wife: [{"children": 2}, {"children": 3}] } this.setState(prevState
我认为问这个问题可能很愚蠢,但请相信我,我是 reactJS 的初学者。有人能解释一下为什么我们在 ReactjS 中使用 prevState 吗?我努力去理解但失败了。 这是我的代码。请帮助我理解
一段时间以来,我一直在努力解决这个问题,但一直没有找到正确的答案。 在下面的代码中: this.setState(prevState => counter: prevState.counter + 1
我刚开始学习 React 和 JavaScript。 在学习本教程时,我得到了一个组件的示例代码,它创建了一个切换按钮。 这是代码的一部分: class Toggle extends React.Co
我有一个主要组件,它使用一些 Prop 调用另一个组件。 // Main Component 单击按钮时,我正在更改状态 (this.state.submitClicked)。 {
修改 prevState 的一部分并在 setState() 中返回该部分是否有真正的缺点? 例子: this.setState(prevState => { prevState.myObje
我正在查看一些示例 React 代码(在 antd 文档中),我注意到它们的代码相当于: this.setState(prevState => { prevState.name = "NewValue
在从 useState Hook 获取的 setState 中返回(已更改的)先前状态似乎不会改变状态。运行以下直接代码片段 function App(){ const [state, setSt
我过去使用过 componentDidUpdate() 并且它按预期工作。 然而,这一次我做到了 componentDidUpdate(prevProps, prevState) { if (
看到一个奇怪的问题,其中 prevState 在 componentDidUpdate 中未定义,但在组件在浏览器中运行时定义。 我在构造函数中设置了状态,并且在 componentDidUpdate
我正在查看这个来自 this article 的 redux super 基本实现的示例。我理解它,除了调度使用 prevState 的地方。首先这个函数从哪里获取 prevState ?这与计数器需
我是一名优秀的程序员,十分优秀!