React · Senior · Question 14 of 75
What is useEffectEvent for?
- ✓Correct answer: To read the latest props and state in an effect without making them dependencies
- 2To subscribe an effect to native DOM events without
addEventListener - 3To run an effect only in response to a click, not after render
Explanation
Example: a chat connection effect depends on roomId, and on connection you need to show a notification using the current theme. Adding theme to dependencies would reconnect the chat on theme change. const onConnected = useEffectEvent(() => notify(theme)) always sees the fresh theme but isn't reactive.