Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useLatest => useLatestRef #1965

Open
jamesplease opened this issue Apr 26, 2021 · 2 comments
Open

useLatest => useLatestRef #1965

jamesplease opened this issue Apr 26, 2021 · 2 comments

Comments

@jamesplease
Copy link

jamesplease commented Apr 26, 2021

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?

@pastelmind
Copy link

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.

@jamesplease
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants