react
react copied to clipboard
Feature: useEffectEvent should be useCallback without dependency array
The experimental useEffectEvent is generally something useCallback should have been without depedency array or with empty dependency array to match the behavior of useEffect. Introducing new name will just bring more confusion since it's very hard to image, what useEffectEvent should do...🙏🏻✌🏻👀
um, cool
Seems useless why not just pass an empty array in your useEffect to render only once?
@bayareaunicorn That's what it's typically combined with, but if you want to access actual values from a callback provided there, that's when you would want to use it. Despite it's name, experimental useEffectEvent is not an equivalent to useEffect, but to a useCallback, in a sense. That's what this issue is about, that people will probably be confused by it's name...
I see perhaps useChildCallback or useCallbackChild would be a better fit?
useCallback Use Case: It is used to prevent unnecessary re-renders of child components by memoizing a function instance and changing the callback only when one of its dependencies changes .
useEffectEvent Use Case: It provides a more powerful alternative to useCallback with no dependencies, allowing for constant reference to a function and preventing unnecessary re-renders of child components
@bayareaunicorn Well, useChildCallback is probably better then useEffectEvent, but considering that people are alredy used to that returned reference from useHooks changes as often as some of it's dependencies, I think that just useCallback without any dependencies would communicate that intention better with respect to already memorized rules.
I think that would be unwise, the rules of useEffectEvent are that it's only to be called from effects and not passed to child components. useCallback does not have that limitation. Having semantics diverge this massively by just (not) passing a dependency array in, would lead to confusion.
@Gregoor Fair point. So maybe useEffectCallback would be better choice, since calling it Event is quite misleading as well...
Yeah that maps closer to my conceptualization of it as well. I think the React team's lingo might diverge here from ours though. I would be curious to read a team member's take on this.
We're going to be iterating on this API a bit more after 19 so I wouldn't spend too much time bike-shedding the name just yet. The current distinction makes sense because the function returned from useCallback can be passed down, but useEffectEvent can only be used locally, and it would be confusing to have the same API have different prop passing semantics. We have some ideas for how to remove that restriction so we're going to investigate those options.
I'll share more when we know more.
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!