# @react-hook/switch

> A React hook for controlling a boolean value with toggle, on, and off callbacks

Latest version **1.3.3** (published 2021-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @react-hook/switch
pnpm add @react-hook/switch
yarn add @react-hook/switch
bun add @react-hook/switch
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2021-01-19 |
| First published | 2019-08-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 33.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1530 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | react, react hook, react hooks, hooks, switch, toggle, toggle hook, switch hook, react switch hook, react toggle hook |

## Links

- npm: https://www.npmjs.com/package/@react-hook/switch
- Repository: https://github.com/jaredLunde/react-hook
- Homepage: https://github.com/jaredLunde/react-hook/tree/master/packages/switch#readme
- Issues: https://github.com/jaredLunde/react-hook/issues
- npm.io page: https://npm.io/package/@react-hook/switch

## Dependencies (1)

- [@react-hook/latest](https://npm.io/package/@react-hook/latest.md) ^1.0.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.3.3 (latest) — 2021-01-19
- 1.3.2 — 2021-01-19
- 1.3.1 — 2021-01-17
- 1.3.0 — 2020-07-01
- 1.2.0 — 2020-06-07
- 1.1.1 — 2020-05-31
- 1.1.0 — 2020-05-30
- 1.0.4 — 2019-12-09
- 1.0.3 — 2019-12-09
- 1.0.2 — 2019-08-29
- 1.0.1 — 2019-08-29
- 1.0.0 — 2019-08-29

## README

<hr>
<div align="center">
  <h1 align="center">
    useSwitch()
  </h1>
</div>

<p align="center">
  <a href="https://bundlephobia.com/result?p=@react-hook/switch">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@react-hook/switch?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Types" href="https://www.npmjs.com/package/@react-hook/switch">
    <img alt="Types" src="https://img.shields.io/npm/types/@react-hook/switch?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Build status" href="https://travis-ci.com/jaredLunde/react-hook">
    <img alt="Build status" src="https://img.shields.io/travis/com/jaredLunde/react-hook?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@react-hook/switch">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/@react-hook/switch?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="License" href="https://jaredlunde.mit-license.org/">
    <img alt="MIT License" src="https://img.shields.io/npm/l/@react-hook/switch?style=for-the-badge&labelColor=24292e">
  </a>
</p>

<pre align="center">npm i @react-hook/switch</pre>
<hr>

A React hook for controlling a boolean value with toggle, on, and off callbacks. This
is extremely useful for adding controlled/uncontrolled component behavior to components
like `<Checkbox/>`, `<Toggle/>`, `<Modal/>`, etc.

## Quick Start

```jsx harmony
import useSwitch from '@react-hook/switch'

// Basic usage
const Component = (props) => {
  const [value, toggle] = useSwitch(false /*default value*/)

  return (
    <>
      <span>Value: {value}</span>
      {/* toggles the current value to its opposite*/}
      <button onClick={toggle}>Toggle</button>
      {/* toggles the current value to true*/}
      <button onClick={toggle.on}>On</button>
      {/* toggles the current value to false*/}
      <button onClick={toggle.off}>On</button>
    </>
  )
}

// Creating a toggle component with a controlled and uncontrolled
// value pattern
const Toggle = ({value: controlledValue, defaultValue, onChange}) => {
  const [value, toggle] = useSwitch(defaultValue, controlledValue, onChange)

  return (
    <>
      <span>Value: {value}</span>
      <button onClick={toggle}>Toggle</button>
      <button onClick={toggle.on}>On</button>
      <button onClick={toggle.off}>On</button>
    </>
  )
}
```

## API

### `useSwitch(defaultValue?, controlledValue?, onChange?)`

```
function useSwitch(defaultValue?: boolean, controlledValue?: boolean, onChange?: (value: boolean, prevValue: boolean) => any): readonly [boolean, (() => void) & {
    on: () => void;
    off: () => void;
}]
```

#### Arguments

| Argument        | Type                      | Default     | Required? | Description                                                                     |
| --------------- | ------------------------- | ----------- | --------- | ------------------------------------------------------------------------------- |
| defaultValue    | `boolean`                 | `false`     | No        | Sets the default value of the switch                                            |
| controlledValue | `boolean`                 | `undefined` | No        | Sets the controlled value of the switch, which will override the `defaultValue` |
| onChange        | `(value: boolean) => any` | `undefined` | No        | A callback invoked whenever toggle callbacks that change state are invoked      |

#### Returns `[value: boolean, toggle: Toggle]`

| Variable | Type                                             | Description                                                                |
| -------- | ------------------------------------------------ | -------------------------------------------------------------------------- |
| value    | `boolean`                                        | Defines the initial value                                                  |
| toggle   | `() => void & {on: () => void, off: () => void}` | If the `value` is `true`, calling this will make it `false` and vice-versa |

#### `() => void & {on: () => void, off: () => void}`

| Method | Type         | Description                     |
| ------ | ------------ | ------------------------------- |
| on     | `() => void` | Switches the `value` to `true`  |
| off    | `() => void` | Switches the `value` to `false` |

## LICENSE

MIT

---
_Source: https://npm.io/package/@react-hook/switch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
