Skip to content

Commit

Permalink
feat: create useToggle hook
Browse files Browse the repository at this point in the history
  • Loading branch information
juliencrn committed Oct 13, 2022
1 parent dbec137 commit 99a1882
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
![npm bundle size](https://img.shields.io/bundlephobia/minzip/usehooks-ts)
![npm](https://img.shields.io/npm/v/usehooks-ts)<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-85-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br />
Expand Down Expand Up @@ -66,6 +67,7 @@
- [`useStep()`](https://usehooks-ts.com/react-hook/use-step)
- [`useTernaryDarkMode()`](https://usehooks-ts.com/react-hook/use-ternary-dark-mode)
- [`useTimeout()`](https://usehooks-ts.com/react-hook/use-timeout)
- [`useToggle()`](https://usehooks-ts.com/react-hook/use-toggle)
- [`useUpdateEffect()`](https://usehooks-ts.com/react-hook/use-update-effect)
- [`useWindowSize()`](https://usehooks-ts.com/react-hook/use-window-size)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"test:watch": "npm run test -- --watch --silent",
"test:coverage": "ts-node ./scripts/coverage.ts",
"clean": "rimraf -rf ./dist",
"format": "prettier --check \"**/*.{json,md,mdx,css,scss,yaml,yml}\"",
"format": "prettier --write \"**/*.{json,md,mdx,css,scss,yaml,yml}\"",
"lint": "eslint '**/*.{js,jsx,ts,tsx}'",
"types-check": "tsc --noEmit",
"bump": "bump --tag 'usehooks-ts@%s' --commit 'release: usehooks-ts@%s'",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export { default as useTernaryDarkMode } from './useTernaryDarkMode/useTernaryDa
export * from './useTernaryDarkMode/useTernaryDarkMode'
export { default as useTimeout } from './useTimeout/useTimeout'
export * from './useTimeout/useTimeout'
export { default as useToggle } from './useToggle/useToggle'
export * from './useToggle/useToggle'
export { default as useUpdateEffect } from './useUpdateEffect/useUpdateEffect'
export * from './useUpdateEffect/useUpdateEffect'
export { default as useWindowSize } from './useWindowSize/useWindowSize'
Expand Down
4 changes: 4 additions & 0 deletions src/useBoolean/useBoolean.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ date: '2021-07-29'
---

A simple abstraction to play with a boolean, don't repeat yourself.

Related hooks:

- [`useToggle()`](/react-hook/use-toggle)
1 change: 1 addition & 0 deletions src/useCountdown/useCountdown.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ NEW VERSION: A simple countdown implementation. Accepts `countStop`(new), `count
Related hooks:

- [`useBoolean()`](/react-hook/use-boolean)
- [`useToggle()`](/react-hook/use-toggle)
- [`useCounter()`](/react-hook/use-counter)
- [`useInterval()`](/react-hook/use-interval)
20 changes: 20 additions & 0 deletions src/useToggle/useToggle.demo.tsx
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>
</>
)
}
10 changes: 10 additions & 0 deletions src/useToggle/useToggle.mdx
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)
59 changes: 59 additions & 0 deletions src/useToggle/useToggle.test.ts
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)
})
})
13 changes: 13 additions & 0 deletions src/useToggle/useToggle.ts
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
2 changes: 0 additions & 2 deletions templates/plop/hooks/hook/demo.tsx.hbs
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() {
Expand Down
2 changes: 0 additions & 2 deletions templates/plop/hooks/hook/hook.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {} from 'react'

function {{camelCase name}}(): [number, () => number] {
const value = 2
const method = () => 2
Expand Down

0 comments on commit 99a1882

Please sign in to comment.