Skip to content

Commit

Permalink
fix(useBoolean): default value is converted to boolean type (#2108)
Browse files Browse the repository at this point in the history
  • Loading branch information
KangXinzhi authored Mar 14, 2023
1 parent 1ef70a2 commit 86ec596
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions packages/hooks/src/useBoolean/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ describe('useBoolean', () => {
});

it('test on default value', () => {
const hook = setUp(true);
expect(hook.result.current[0]).toBe(true);
const hook1 = setUp(true);
expect(hook1.result.current[0]).toBe(true);
const hook2 = setUp();
expect(hook2.result.current[0]).toBe(false);
// @ts-ignore
const hook3 = setUp(0);
expect(hook3.result.current[0]).toBe(false);
// @ts-ignore
const hook4 = setUp('');
expect(hook4.result.current[0]).toBe(false);
// @ts-ignore
const hook5 = setUp('hello');
expect(hook5.result.current[0]).toBe(true);
});
});
2 changes: 1 addition & 1 deletion packages/hooks/src/useBoolean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Actions {
}

export default function useBoolean(defaultValue = false): [boolean, Actions] {
const [state, { toggle, set }] = useToggle(defaultValue);
const [state, { toggle, set }] = useToggle(!!defaultValue);

const actions: Actions = useMemo(() => {
const setTrue = () => set(true);
Expand Down

0 comments on commit 86ec596

Please sign in to comment.