Skip to content

Commit

Permalink
feat: Typings for useDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich authored Jan 20, 2020
2 parents 1ec99eb + 83f4c46 commit fa0f53b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/useDefault.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useState } from 'react';

const useDefault = (defaultValue, initialValue): [any, (nextValue?: any) => void] => {
const [value, setValue] = useState(initialValue);
const useDefault = <TStateType>(defaultValue: TStateType, initialValue: TStateType | (() => TStateType)) => {
const [value, setValue] = useState<TStateType | undefined | null>(initialValue);

if (value === undefined || value === null) {
return [defaultValue, setValue];
return [defaultValue, setValue] as const;
}

return [value, setValue];
return [value, setValue] as const;
};

export default useDefault;

0 comments on commit fa0f53b

Please sign in to comment.