-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
[Bug report] Remove useLatest #2328
Comments
When I was writing something like this, I would wrap it in |
Wouldn't it be fixed by a const useLatest = <T>(value: T): { readonly current: T } => {
const ref = useRef(value);
useLayoutEffect(() => ref.current = value, [value]);
return ref;
}; EDIT: it's working slightly differently though so not sure it would cover all use cases and it'd probably warrant a breaking change. |
I use these two hooks frequently in my projects: interface LatestRef<T> {
readonly current: T
}
function useLatestRef<T>(value: T): LatestRef<T> {
const ref = useRef(value)
useEffect(() => {
ref.current = value
})
return ref
}
function useLayoutLatestRef<T>(value: T): LatestRef<T> {
const ref = useRef(value)
useLayoutEffect(() => {
ref.current = value
})
return ref
} These hooks update the ref value at different timings, which can be important when the ref is used by another Since people rely on |
react-use/src/useLatest.ts
Lines 3 to 7 in 3685b75
Although the current React documentation doesn't mention it, according to the React beta documentation:
The text was updated successfully, but these errors were encountered: