【发布时间】:2020-01-05 18:20:30
【问题描述】:
类型 1:
const [count, setCount] = useState(initialCount);
<button onClick={() => setCount(count + 1)}>+</button>
类型 2:
const [count, setCount] = useState(initialCount);
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
虽然上面两个代码给出了相同的结果,但它们之间到底有什么区别以及为什么第二种方法比第一种更受欢迎
【问题讨论】:
-
查看answer
-
这能回答你的问题吗? What is prevState in ReactJS?
标签: reactjs react-hooks