reactjs - useState React 实现
问题描述
我看到了两种不同的 useState 实现,我想知道它们的区别。
const [count, setCount] = useState(0);
...
// First: using var from useState
setCount(count + 1);
// Second: using prevState in a function
setCount(count => count + 1);
解决方案
// Will not be re fired until UI is re rendered e.g you have a cart and you wanna increment the number of products in the cart. That way you can ensure that in case the user hit the increment button twice by accident the number wont increment twice before it‘s re rendered
setCount(count + 1);
// Will update count twice unless a rerendered were completed
setCount(count => count + 1);
推荐阅读
- batch-file - 在批量查询中使用变量
- java - 调整窗口大小时 LineChart 中的 JavaFX 多边形出现故障
- php - 如果检查了贝宝选项,请删除表单验证 Woocommerce
- android - 以编程方式动态更改 TextInputLayout 的焦点颜色
- linux - Bash 在 CSV 列中选择多个值
- sql-server - 将选定列从 SQL 加载到 PySpark
- c# - IList clear 不能按预期工作(空项目)
- javascript - 获取具有唯一值的数组的长度
- ubuntu - Nginx 为远程磁盘提供服务
- c# - 如何从内部代码生成 IIS machineKey?