Created
October 31, 2023 05:42
-
-
Save GalindoSVQ/7e7f6aa854348564dc18bdc59732092f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState } from 'react'; | |
| export default function useDefault<T>(initialState: T, defaultState: T) { | |
| const [value, setValue] = useState<T | null | undefined>(initialState); | |
| if (value === null || typeof value === 'undefined') { | |
| return [defaultState, setValue]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
StackBlitz link: https://stackblitz.com/edit/vitejs-vite-qsqclq?file=src%2FuseDefault.ts