mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
Also reduce max line width to 100. And remove `lint:types` step for commit sequence, it bothers when committing incomplete (wip) changes.
17 lines
401 B
TypeScript
17 lines
401 B
TypeScript
import { useState } from 'react';
|
|
|
|
const useDefault = <TStateType>(
|
|
defaultValue: TStateType,
|
|
initialValue: TStateType | (() => TStateType)
|
|
) => {
|
|
const [value, setValue] = useState<TStateType | undefined | null>(initialValue);
|
|
|
|
if (value === undefined || value === null) {
|
|
return [defaultValue, setValue] as const;
|
|
}
|
|
|
|
return [value, setValue] as const;
|
|
};
|
|
|
|
export default useDefault;
|