useDefault
Sets a default value to a state when it is null or undefined
The useDefault
hook is helpful when you want to assign a default value to a state which is likely to be undefined
or null
in future.
It ensures that the state never reaches the undefined
or null
values.
useDefault
Current Value: 1
Usage
Import the hook from @abhushanaj/react-hooks
and use in required component.
Properties
The defaultValue cannot be null
or undefined
.
In such cases, and error is throw with a message: Default value cannot be undefined
or Default value cannot be null
.
API Reference
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
initialValue | InitialValues<T> | The variable to keep track of. | N/A |
defaultValue | T | The default value when state becomes null or undefined . | N/A |
Return Value
The hook returns the same signature of React.useState()
hook. It is a tuple consistent of [state, stateUpdateFn]
Parameter | Type | Description | Default |
---|---|---|---|
state | T | The current state value. | N/A |
stateUpdateFn | StateUpdateFn | The state updater function. | N/A |
Types Reference
1. InitialValues<T>
InitialValues<T>
: T | null | undefined
;
2. StateUpdateFn
React.Dispatch<React.SetStateAction<InitialValues<T>>>