forked from juliencrn/usehooks-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useToggle } from '..' | ||
|
||
export default function Component() { | ||
const [value, toggle, setValue] = useToggle() | ||
|
||
// Just an example to use "setValue" | ||
const customToggle = () => setValue((x: boolean) => !x) | ||
|
||
return ( | ||
<> | ||
<p> | ||
Value is <code>{value.toString()}</code> | ||
</p> | ||
<button onClick={() => setValue(true)}>set true</button> | ||
<button onClick={() => setValue(false)}>set false</button> | ||
<button onClick={toggle}>toggle</button> | ||
<button onClick={customToggle}>custom toggle</button> | ||
</> | ||
) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: useToggle | ||
date: '2022-10-12' | ||
--- | ||
|
||
A simple abstraction to play with a boolean, don't repeat yourself. | ||
|
||
Related hooks: | ||
|
||
- [`useBoolean()`](/react-hook/use-boolean) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks/dom' | ||
|
||
import useToggle from './useToggle' | ||
|
||
describe('use toggle()', () => { | ||
test('should use toggle be ok', () => { | ||
const { result } = renderHook(() => useToggle()) | ||
const [value, toggle, setValue] = result.current | ||
|
||
expect(value).toBe(false) | ||
expect(typeof toggle).toBe('function') | ||
expect(typeof setValue).toBe('function') | ||
}) | ||
|
||
test('should default value works', () => { | ||
const { result } = renderHook(() => useToggle(true)) | ||
const [value] = result.current | ||
|
||
expect(value).toBe(true) | ||
}) | ||
|
||
test('setValue should mutate the value', () => { | ||
const { result } = renderHook(() => useToggle()) | ||
const [, , setValue] = result.current | ||
|
||
expect(result.current[0]).toBe(false) | ||
|
||
act(() => { | ||
setValue(true) | ||
}) | ||
|
||
expect(result.current[0]).toBe(true) | ||
|
||
act(() => { | ||
setValue(prev => !prev) | ||
}) | ||
|
||
expect(result.current[0]).toBe(false) | ||
}) | ||
|
||
test('toggle should mutate the value', () => { | ||
const { result } = renderHook(() => useToggle()) | ||
const [, toggle] = result.current | ||
|
||
expect(result.current[0]).toBe(false) | ||
|
||
act(() => { | ||
toggle() | ||
}) | ||
|
||
expect(result.current[0]).toBe(true) | ||
|
||
act(() => { | ||
toggle() | ||
}) | ||
|
||
expect(result.current[0]).toBe(false) | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Dispatch, SetStateAction, useCallback, useState } from 'react' | ||
|
||
function useToggle( | ||
defaultValue?: boolean, | ||
): [boolean, () => void, Dispatch<SetStateAction<boolean>>] { | ||
const [value, setValue] = useState(!!defaultValue) | ||
|
||
const toggle = useCallback(() => setValue(x => !x), []) | ||
|
||
return [value, toggle, setValue] | ||
} | ||
|
||
export default useToggle |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import React from 'react' | ||
|
||
import { {{camelCase name}} } from '..' | ||
|
||
export default function Component() { | ||
|
This file contains 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