useDefault

Manage state with default values using useDefault.

Install:

npm i @uidotdev/usehooks

Description:

The useDefault hook behaves similar to useState but with one difference – if the state of the hook is undefined or null, useDefault will default the state to a provided default value.

Parameters

NameTypeDescription
initialValueanyThe initial value of the state returned from useDefault
defaultValueanyThe default value to be used if the state is undefined or null.

Return Values

NameTypeDescription
stateanyThe current state. If the state is undefined or null, defaultValue is returned instead.
setStatefunctionThe state setter function returned from React.useState(). This can be called to update the state.

Demo:

Example:

More Hooks:

Sort: