Skip to content

Commit

Permalink
docs: deprecate useLatest, recommend useLatestRef
Browse files Browse the repository at this point in the history
  • Loading branch information
pastelmind committed Jul 14, 2023
1 parent dbefac8 commit 580dfab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/useLatest.md
Expand Up @@ -4,6 +4,8 @@ React state hook that returns the latest state as described in the [React hooks

This is mostly useful to get access to the latest value of some props or state inside an asynchronous callback, instead of that value at the time the callback was created from.

Note: This hook updates the ref value during rendering, and is therefore potentially unsafe. Use `useLatestRef()` if you want to safely access the latest value. For more information, see "Pitfall" section of [React `useRef()` docs](https://react.dev/reference/react/useRef).

## Usage

```jsx
Expand Down
3 changes: 3 additions & 0 deletions src/useLatest.ts
@@ -1,5 +1,8 @@
import { useRef } from 'react';

/**
* @deprecated Use `useLatestRef` instead
*/
const useLatest = <T>(value: T): { readonly current: T } => {
const ref = useRef(value);
ref.current = value;
Expand Down

0 comments on commit 580dfab

Please sign in to comment.