網站首頁 編程語言 正文
React Hook中useState更新延遲
方法一:去掉useEffect的第二個參數
例如以下代碼 錯誤實例
const[zoom, setZoom] = useState(0);
useEffect(() = >{
? ? document.getElementById('workspace-content').addEventListener('mousewheel', scrollFunc);
},[]);
function scrollFunc(e) {
? ? setZoom(zoom + 5)
}
會出現zoom永遠等于 0+5, 而不是所謂的5, 10 ,15 為什么會這樣呢? 因為useEffect執行時,會創建一個閉包,在每次監聽到mousewheel運行時 閉包內部保存了zoom的初始化值 每次調用的時候都是取的初始化值0 所有會一直為0+5
怎么解決呢?
解決方案: 去掉useEffect中的空數組即可
const[zoom, setZoom] = useState(0);
useEffect(() = >{
? ? document.getElementById('workspace-content').addEventListener('mousewheel', scrollFunc);
? ? return () = >document.getElementById('workspace-content').removeEventListener("mousewheel", scrollFunc); // 記得解綁事件
});
function scrollFunc(e) {
? ? setZoom(zoom + 5)
}
方法二:將改變函數移入useEffect并將第二個參數設置為state
依舊用上面的例子
解決方法:正確示例
useEffect(() = >{
? ? document.getElementById('workspace-content').addEventListener('mousewheel', scrollFunc);
? ? return () = >document.getElementById('workspace-content').removeEventListener("mousewheel", scrollFunc);
? ? function scrollFunc(e) {
? ? ? ? setZoom(zoom + 5) e.preventDefault()
? ? }
},[zoom]);
方法三:使用Ref, 在useEffect內監聽此ref, 并實時跟useState同步
例如下面的代碼 錯誤示例
const [currentIndex, setCurrentIndex] = useState(0)
? const handleLeft = () => {
? ? ?setCurrentIndex(currentIndex+ 1)
? ? ?console.log(currentIndex)
? }
初始化currentIndex為0 每次執行handleLeft函 數是讓currentIndex加1, 之后立即獲取currentIndex的值發現 第一次執行currentIndex = 0
第二次執行currentIndex = 1 每次都跟實際情況差一個 ?查閱資料發現useState必須要執行完react整個生命周期才會獲取最新值
解決方案:用useRef中轉,并實時同步給useState?
const [currentIndex, setCurrentIndex] = useState(0)
const currentIndexRef = useRef(0);
? const handleLeft = () => {
? ? ?currentIndexRef.current += 1
? ? ?console.log(currentIndexRef.current)
? }
useEffect(()=>{
? ? setCurrentIndex(currentIndexRef.current)
},[currentIndexRef.current])
React Hook useState連續更新對象問題
react hook 的useState更新是異步的,所以在連續更新出發useState時會出現問題
eg:?
import React, {useState} from 'react';
?
export default () => {
? ? const [obj, setObj] = useState({
? ? ? ? a: 1,
? ? ? ? b: 2
? ? ? })
?
? ? const changeObj = () => {
? ? ? ? // 連續觸發2次setObj(實際項目不會這樣寫,我這樣寫只是為了模擬連續觸發2次setObj帶來的問題)
? ? ? ? setObj({...obj, a: 2})
? ? ? ? setObj({...obj, b: 3})?
? ? }
? ?return (<div onClick={changeObj}>
? ? ? ? ? // 此時頁面上顯示的值為 {a: 1, b: 3}, 出現該問題的原因是因為useState是異步的,在第二次觸發setObj時,obj還是沒更新a之前的obj,所以出現該結果
? ? ? ? ? ? {JSON.stringify(obj)}
? ? ? ? ? ?</div>)
}
解決此情況的方式,就是在第二次觸發時使用setObj((data) => ({...data, b: 3})) 這樣的方式
import React, {useState} from 'react';
?
export default () => {
? ? const [obj, setObj] = useState({
? ? ? ? a: 1,
? ? ? ? b: 2
? ? ? })
?
? ? const changeObj = () => {
? ? ? ? // 連續觸發2次setObj(實際項目不會這樣寫,為了模擬情況)
? ? ? ? setObj({...obj, a: 2})
? ? ? ? // data標識存儲的是更新a后的對象,用這樣的方式可以解決連續觸發2次帶來的問題
? ? ? ? setObj((data) => ({...data, b: 3}))?
? ? }
? ?return (<div onClick={changeObj}>
? ? ? ? ? // 此時頁面上顯示的值為 {a: 2, b: 3}
? ? ? ? ? ? {JSON.stringify(obj)}
? ? ? ? ? ?</div>)
}
原文鏈接:https://blog.csdn.net/qq_35493664/article/details/105195549
相關推薦
- 2023-01-05 Python中使用jpype調用Jar包中的實現方法_python
- 2022-05-07 Python數據結構之棧詳解_python
- 2022-02-17 奇怪的小知識 - 如何清空一個數組?在不改變地址的情況下呢?
- 2022-11-10 關于docker?cgroups資源限制的問題_docker
- 2022-10-26 Python?Numpy教程之排序,搜索和計數詳解_python
- 2022-06-10 redis?解決庫存并發問題實現數量控制_Redis
- 2023-10-15 獲取當天開始時間零點和結束時間24點的時間以及時間戳
- 2022-04-20 C語言特殊符號的補充理解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支