I used three useEffect to control different states and adding/removing event listeners. But I'm not sure that is a best practice or not?
useEffect(() => {
window.addEventListener('keydown', keyPressHandle)
return () => {
window.removeEventListener('keydown', keyPressHandle)
}
}, [])
useEffect(() => {
// do sth on state_one
}, [state_one])
useEffect(() => {
// do sth on state_two
}, [state_two])
Yes, this is a best practice and Reactjs also recommends it
Tip: Use Multiple Effects to Separate Concerns