https://codepen.io/evan-jin/pen/qBjGWvR
const [hoverItem, setHoverItem] = useState(null)
const timerRef = useRef(null)
const addcursor = useCallback(() => {
console.log('addcursor')
clearTimeout(timerRef.current)
timerRef.current = null
timerRef.current = setTimeout(() => {
document.body.style.cursor = 'wait'
}, 10)
}, [])
const removeCursor = useCallback(() => {
if (timerRef.current === null) return
console.log('removeCursor')
timerRef.current = setTimeout(() => {
document.body.style.cursor = 'default'
}, 500)
}, [])
useEffect(() => {
if (hoverItem) {
addcursor()
} else {
removeCursor()
}
}, [hoverItem])
I wanna keep 'wait cursor' whenver hover on box div but setTimeout triggers when I stop moving cursor.
I tried to use clearTimeout on useEffect return(willUnmount) but doesn't work.
It works if I move in and out on each box component but If I move Cursor fast to the other one, It ends up triggering RemoveCursor setTimeout at the end.
To sum up,
- I wanna keep 'wait cursor' for 500ms when I move cursor out
- After pass through boxs, 'wait cursor' need to stay on box if cursor is on it but not just disappearing
this question looks weird but I just made this simple problem for my real project please anybody help me..!