1 | ;
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = useToggleState;
|
5 |
|
6 | var _react = require("react");
|
7 |
|
8 | /**
|
9 | * Create a state setter pair for a boolean value that can be "switched".
|
10 | * Unlike `useState(false)`, `useToggleState` will automatically flip the state
|
11 | * value when its setter is called with no argument.
|
12 | *
|
13 | * @param initialState The initial boolean value
|
14 | * @returns A tuple of the current state and a setter
|
15 | *
|
16 | * ```jsx
|
17 | * const [show, toggleShow] = useToggleState(false)
|
18 | *
|
19 | * return (
|
20 | * <>
|
21 | * <button onClick={() => toggleShow()}>
|
22 | * Toggle
|
23 | * <button>
|
24 | *
|
25 | * {show && <strong>Now you can see me</strong>}
|
26 | * </>
|
27 | * )
|
28 | *
|
29 | * ```
|
30 | */
|
31 | function useToggleState(initialState) {
|
32 | if (initialState === void 0) {
|
33 | initialState = false;
|
34 | }
|
35 |
|
36 | return (0, _react.useReducer)(function (state, action) {
|
37 | return action == null ? !state : action;
|
38 | }, initialState);
|
39 | } |
\ | No newline at end of file |