react-use icon indicating copy to clipboard operation
react-use copied to clipboard

useLatest => useLatestRef

Open jamesplease opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe.

The name useLatest does not indicate that it returns a ref.

Describe the solution you'd like

Rename useLatest to useLatestRef.

Describe alternatives you've considered

Leave the name as is.


Migration Path Considerations

This is a Big Breaking Change© and could cause a lot of frustration. Likely more frustration than it's worth if useLatest just vanishes one day. But I think it's doable in a way that minimizes frustration:

Step one: a minor release that introduces useLatestRef

In a minor release to this library, introduce useLatestRef as an alias to useLatest. Update the docs to describe useLatestRef as the recommended alias of useLatest.

Step two: a major release that deprecates useLatest

In the next major release, deprecate useLatest. Remove it from the docs (or mark it as deprecated in the docs), but keep it in the code and add a dev mode warning:

if (process.env.NODE_ENV !== 'production') {
  // Just an example msg lol
  console.warn('useLatest is deprecated and will be removed in the next major release. Please import useLatestRef instead.');
}

(you would want to cache the message so it only logs one time, like React logs)

Step three: another major release that removes useLatest

A long time from now, you cut a 2nd major release that removes useLatest altogether.


Is it worth the effort? idk, what do you think?

jamesplease avatar Apr 26 '21 17:04 jamesplease

useLatest() is potentially unsafe because it updates the ref during rendering. Since people may depend on its implementation behavior, it may be better to introduce a safer alternative named useLatestRef() and deprecate useLatest().

I submitted #2509 which does exactly this.

pastelmind avatar Jul 14 '23 07:07 pastelmind

Incredible PR, @pastelmind ! The version of useLayoutRef that you’ve PR’d is actually what I use in my own apps, as I prefer to follow the React best practices that you mentioned.

I’d love to see that merged.

jamesplease avatar Jul 14 '23 19:07 jamesplease