Skip to content

Commit ce6de24

Browse files
committed
feat: 🎸 add useBoolean hook
1 parent 8e81e8f commit ce6de24

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
- [`useGetSet`](./docs/useGetSet.md) — returns state getter `get()` instead of raw state.
7676
- [`useObservable`](./docs/useObservable.md) — tracks latest value of an `Observable`.
7777
- [`useSetState`](./docs/useSetState.md) — creates `setState` method which works like `this.setState`. [![][img-demo]](https://codesandbox.io/s/n75zqn1xp0)
78-
- [`useToggle`](./docs/useToggle.md) — tracks state of a boolean.
78+
- [`useToggle` and `useBoolean`](./docs/useToggle.md) — tracks state of a boolean.
7979
- [`useCounter`](./docs/useCounter.md) — tracks state of a number.
8080
- [`useList`](./docs/useList.md) — tracks state of an array.
8181
- [`useMap`](./docs/useMap.md) — tracks state of an object.

docs/useToggle.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
React state hook that tracks value of a boolean.
44

5+
`useBoolean` is an alias for `useToggle`.
6+
57

68
## Usage
79

810
```jsx
9-
import {useToggle} from 'react-use';
11+
import {useToggle, useBoolean} from 'react-use';
1012

1113
const Demo = () => {
1214
const [on, toggle] = useToggle(true);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {useBoolean} from '..';
4+
import ShowDocs from '../util/ShowDocs';
5+
6+
const Demo = () => {
7+
const [on, toggle] = useBoolean(true);
8+
9+
return (
10+
<div>
11+
<div>{on ? 'ON' : 'OFF'}</div>
12+
<button onClick={() => toggle()}>Toggle</button>
13+
<button onClick={() => toggle(true)}>set ON</button>
14+
<button onClick={() => toggle(false)}>set OFF</button>
15+
</div>
16+
);
17+
};
18+
19+
storiesOf('useBoolean', module)
20+
.add('Docs', () => <ShowDocs md={require('../../docs/useToggle.md')} />)
21+
.add('Demo', () =>
22+
<Demo/>
23+
)

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import useAsync from './useAsync';
22
import useAudio from './useAudio';
33
import useBattery from './useBattery';
4+
import useBoolean from './useBoolean';
45
import useCounter from './useCounter';
56
import useCss from './useCss';
67
import useFavicon from './useFavicon';
@@ -38,6 +39,7 @@ export {
3839
useAsync,
3940
useAudio,
4041
useBattery,
42+
useBoolean,
4143
useCounter,
4244
useCss,
4345
useFavicon,

src/useBoolean.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import useBoolean from './useToggle';
2+
3+
export default useBoolean;

0 commit comments

Comments
 (0)