React Native · APIs and utilities
Hooks
Writes text to the clipboard and keeps a "copied" state for a short while (same API as useClipboard on web).
Import
useClipboard
functionWrites text to the clipboard and keeps a "copied" state for a short while (same API as useClipboard on web). Uses the library's own clipboard module.
For a ready-made button see CopyButton.
Examples
useControllableState
functionState management for components usable both controlled (value + onChange) and uncontrolled (defaultValue). Gives the same behavior everywhere, like Dialog's open or Tabs' value prop.
Examples
useDebounce
functionA copy of the value updated delay ms after its last change. To avoid sending a request on every keystroke in a search box.
Examples
useDebouncedCallback
functionA debounced version of a function. The returned function is stable; even if the inner function changes on every render, the current one is called. The pending call is cancelled when the component unmounts.
Examples
useDisclosure
functionState of anything that opens and closes (Dialog, Sheet, Popover, Collapsible, menus). Instead of writing useState(false) + three separate functions.
Examples
useScrollTracker
functionTracks the offset and direction of a scrollable component without re-rendering the component that renders it. Fab (scrollBehavior) and BackToTop subscribe to it; only they re-render. On web the browser's scroll event does this; on native the scroll component must be bound explicitly.
Examples
useScrollValue
functionReads a value selected from a ScrollTracker; re-renders only when that value changes (e.g. (s) => s.away). An empty state without a tracker.
ClipboardValue
typeValue to write to the clipboard (the same type as web): plain text or rich content. The mobile clipboard does not carry formatting; text is copied.
UseClipboardOptions
interfaceOptions of the useClipboard hook.
| Name | Type | Default | Description |
|---|---|---|---|
timeout? | number | 2000 | After how many ms copied returns to false. With 0 it does not reset by itself. |
sensitive? | boolean | false | Sensitive data (password, one-time code): hidden in the preview on Android 13+, not sent to other devices on iOS. See ClipboardSetOptions. |
expiresIn? | number | Removed from the clipboard after this many ms (if the user has not copied something else). | |
onCopy? | (text: string) => void | Called when copying succeeds. | |
onError? | (error: unknown) => void | Called when copying fails. |
UseClipboardResult
interfaceValue returned by the useClipboard hook.
| Name | Type | Description |
|---|---|---|
copy | (value: ClipboardValue | (() => ClipboardValue | Promise<ClipboardValue>)) => Promise<boolean> | Writes a value to the clipboard; returns true on success. The value can also be a function or a Promise (e.g. a share link from the server). |
copied | boolean | The last copy succeeded and the timeout has not passed. |
error | unknown | Error of the last copy; null after a successful copy. |
reset | () => void | Resets copied and error. |
UseControllableStateOptions
interfaceOptions of the useControllableState hook.
| Name | Type | Description |
|---|---|---|
value? | T | undefined | Value given from outside. When not undefined the component is controlled. |
defaultValue | T | Initial value in uncontrolled use. |
onChange? | ((value: T) => void) | undefined | Called whenever the value changes; in both controlled and uncontrolled use. |
UseDebouncedCallbackOptions
interfaceOptions of the useDebouncedCallback hook.
| Name | Type | Default | Description |
|---|---|---|---|
leading? | boolean | false | Run immediately on the first call, then stay silent during the wait. |
trailing? | boolean | true | Run with the last call when the wait ends. |
maxWait? | number | Runs at least once within this time (ms) even if calls never stop. Given the same as delay, it becomes a throttle: for scrolling, resizing, pointer tracking. |
DebouncedFunction
interfaceThe function returned by the useDebouncedCallback hook.
| Name | Type | Description |
|---|---|---|
cancel | () => void | Cancels the pending call. |
flush | () => void | Runs the pending call immediately (e.g. save when leaving the page). |
pending | () => boolean | Whether a call is pending. |
UseDisclosureOptions
interfaceOptions of the useDisclosure hook.
| Name | Type | Default | Description |
|---|---|---|---|
defaultOpen? | boolean | false | Open initially? |
open? | boolean | State managed from outside; when given the hook is controlled. | |
onOpenChange? | (open: boolean) => void | Called whenever the state changes. |
UseDisclosureResult
interfaceValue returned by the useDisclosure hook.
| Name | Type | Description |
|---|---|---|
open | boolean | Whether it is open. |
setOpen | (open: boolean) => void | Sets the state directly. |
show | () => void | Opens. Stable; no need to add it to dependency lists. |
hide | () => void | Closes. Stable. |
toggle | () => void | Closes if open, opens if closed. Stable. |
props | { open: boolean; onOpenChange: (open: boolean) => void; } | Props to spread onto overlay components: <Dialog {...modal.props}>, <Sheet {...panel.props}>. |
Scrollable
interfaceReference of a scrollable component: ScrollView, FlatList, VirtualList…
| Name | Type | Description |
|---|---|---|
scrollTo? | (options: { x?: number; y?: number; animated?: boolean; }) => void | ScrollView scroll method. |
scrollToOffset? | (options: { offset: number; animated?: boolean; }) => void | FlatList scroll method. |
ScrollTrackerState
interfaceSnapshot state of a ScrollTracker.
| Name | Type | Description |
|---|---|---|
y | number | Scroll offset (vertical, dp). |
viewport | number | Height of the visible area. |
content | number | Total height of the content. |
direction | "up" | "down" | "none" | Last significant scroll direction. |
away | boolean | Whether the user scrolled down into the content (false when scrolling up or returning to the top). The Fab's hiding/shrinking uses this. |
ScrollTracker
interfaceResult of useScrollTracker: passed to the scrollable component and to the trackers.
| Name | Type | Description |
|---|---|---|
ref | { current: Scrollable | null; } | ref of the scrollable component (to scroll back to the top). |
onScroll | (event: NativeSyntheticEvent<NativeScrollEvent>) => void | onScroll of the scrollable component; use it with scrollEventThrottle={16}. |
get | () => ScrollTrackerState | Snapshot state (does not trigger a render). |
subscribe | (listener: (state: ScrollTrackerState) => void) => () => void | Called when the state changes; returns a function that ends the subscription. |
scrollTo | (y?: number, animated?: boolean) => void | Scrolls to the top (or to y). |