@kodingdotninja/use-toggle
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

use-toggle

npm packagephobia/install packagephobia/publish

Toggle custom hook and component wrapper for React 🔦


Table of contents


Installing

pnpm install @kodingdotninja/use-toggle

Example usage

Toggle hook - useToggle()

Use it as your usual hooks. disable, enable, or toggle does not accept parameters so you can use it on onClick handlers.

import { useToggle } from "@kodingdotninja/use-toggle";

function App() {
  const { state, disable, enable, set, toggle } = useToggle();
  return (
    <div>
      <span>State: {state ? "enabled" : "disabled"}</span>
      <button onClick={disable}>toggle</button>
      <button onClick={enable}>toggle</button>
      <button onClick={() => set(true)}>set true</button>
      <button onClick={toggle}>toggle</button>
    </div>
  );
}

Toggle wrapper - <ToggleWrap />

Component which wraps the children with its internal hooks. Use this if you do not want to declare another component and just wrap it.

import { ToggleWrap } from "@kodingdotninja/use-toggle";

function App() {
  return (
    <ToggleWrap>
      {({ state, disable, enable, set, toggle }) => (
        <div>
          <span>State: {state ? "enabled" : "disabled"}</span>
          <button onClick={disable}>toggle</button>
          <button onClick={enable}>toggle</button>
          <button onClick={() => set(true)}>set true</button>
          <button onClick={toggle}>toggle</button>
        </div>
      )}
    </ToggleWrap>
  );
}

<ClientOnly />

Same as <ToggleWrap enableOnMount initialState={false} />, usually used on client-side only components.

import { ClientOnly } from "@kodingdotninja/use-toggle";

function App() {
  return (
    <ClientOnly>
      <div>...</div>
    </ClientOnly>
  );
}

API

Read the full API documentation on https://kodingdotninja.github.io/use-toggle/.

Maintainers

License

MIT License, Copyright (c) 2024 Koding Ninja

Package Sidebar

Install

npm i @kodingdotninja/use-toggle

Weekly Downloads

145

Version

1.0.1

License

MIT

Unpacked Size

10.8 kB

Total Files

7

Last publish

Collaborators

  • grikomsn
  • ri7nz