# use-toggle-many

> React hook for toggling multiple items in a single component

Latest version **1.1.1** (published 2019-03-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-toggle-many
pnpm add use-toggle-many
yarn add use-toggle-many
bun add use-toggle-many
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2019-03-18 |
| First published | 2019-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 148.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Danko Milutinovic |
| Maintainers | danko |
| Keywords | toggle, react-hook, react, state-management |

## Links

- npm: https://www.npmjs.com/package/use-toggle-many
- Repository: https://github.com/dankoknad/use-toggle-many
- Issues: https://github.com/dankoknad/use-toggle-many/issues
- npm.io page: https://npm.io/package/use-toggle-many

## 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.1.1 (latest) — 2019-03-18
- 1.1.0 — 2019-03-18
- 1.0.8 — 2019-03-17
- 1.0.7 — 2019-03-17
- 1.0.6 — 2019-03-17
- 1.0.5 — 2019-03-17
- 1.0.4 — 2019-03-17
- 1.0.3 — 2019-03-17
- 1.0.2 — 2019-03-17
- 1.0.1 — 2019-03-17
- 1.0.0 — 2019-03-17

## README

# useToggleMany - custom react hook

## Install:

With yarn:

```bash
yarn add use-toggle-many -S
```

With npm:

```bash
npm i use-toggle-many -S
```

## Api:

```javascript
const [handleActive, isActive, active, handleMany] = useToggleMany(arr)
```

handleActive:  `(idOrIndex: string | number) => void`

isActive: `(idOrIndex: string | number) => boolean`

active: `Array<string | number>`

handleMany: `(arr: (string | number)[]) => void`

arr?: `(number | string)[]`


## Demos

Basic usage: [demo](https://codesandbox.io/s/xow466o03o)

With initiall values set and optionall variable `active` used: [demo](https://codesandbox.io/s/x74q4wqqvo)

With "clear all" and "update many at once" buttons: [demo](https://codesandbox.io/s/m94q1q9x3j)

## Examples

Basic:

```javascript
import React from 'react'
import ReactDOM from 'react-dom'
import { useToggleMany } from 'use-toggle-many'

function SomeComponent({ fruits }) {
  const [handleActive, isActive] = useToggleMany()

  return (
    <div>
      <ul>
        {fruits.map(fruit => (
          <li
            key={fruit}
            style={{ color: isActive(fruit) ? "#6ada55" : "#222" }}
          >
            <input
              type="checkbox"
              onChange={() => handleActive(fruit)}
              checked={isActive(fruit)}
            />
            {fruit}: {isActive(fruit) ? "on" : "off"} - toggle by name ({fruit})
          </li>
        ))}
      </ul>
    </div>
  )
}

```

With `initiallState` and active variable `used`:

```javascript
import React, { useState } from 'react'
import { useToggleMany } from 'use-toggle-many'

function SomeComponent() {
  const [handleActive, isActive, active] = useToggleMany([0, 2, 4, 14])

  return (
    <div>
      <ul>
        {"INLOVEWITHHOOKS".split("").map((char, index) => (
          <li key={index} style={{ color: isActive(index) ? "#6ada55" : "#222" }}>
            <input
              type="checkbox"
              onChange={() => handleActive(index)}
              checked={isActive(index)}
            />
            {char}: {isActive(index) ? "on" : "off"} - index ({index})
          </li>
        ))}
      </ul>

      <p>Currently active: {active.length}</p>
    </div>
  )
}

```

With "clear all" and "update many at once" buttons:

```javascript
function SomeComponent({ fruits }) {
  const [handleActive, isActive, active, handleMany] = useToggleMany();

  return (
    <div>
      <ul>
        {fruits.map(fruit => (
          <li
            key={fruit}
            style={{ color: isActive(fruit) ? "#6ada55" : "#222" }}
          >
            <input
              type="checkbox"
              onChange={() => handleActive(fruit)}
              checked={isActive(fruit)}
            />
            {fruit}: {isActive(fruit) ? "on" : "off"} - toggle by name ({fruit})
          </li>
        ))}
      </ul>
      <button onClick={() => handleMany([])}>Clear all</button>
      <button onClick={() => handleMany(["Grapefruit", "Blueberries"])}>
        Set Grapefruit & Blueberries
      </button>
    </div>
  );
}
```

---
_Source: https://npm.io/package/use-toggle-many · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
