useDefault
A custom React hook that manages a stateful value with a default fallback.
useDefault is a helpful custom React hook designed to manage stateful values with a default fallback when the value is set to null or undefined.
This hook is particularly useful when you need to maintain state values that might be reset or cleared, but should fall back to a predefined default rather than being null or undefined.
Loading...
Installation
pnpm dlx shadcn@latest add https://shaddy-docs.vercel.app/r/use-default
API
▸ useDefault(initialValue, defaultValue): [T, (newValue: Nullable<T>) => void]
| Name | Type | Required | Description |
|---|---|---|---|
| initialValue | T | Yes | The initial value of the state. |
| defaultValue | T | Yes | The fallback value when state is null or undefined. |
Returns [value, setValue]:
value(T): The current state value.setValue(function): A function that updates the state value. If called withnullorundefined, it will use the default value instead.