useToggle

Toggle a boolean value.

https://www.shaktools.com/shak-hooks
State

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useToggle } from "@shak-hooks/usehooks";
Source: packages/react/src/useToggle.ts

Signature

signature.ts
export function useToggle(initialValue: boolean = false)

Parameters

NameTypeOptionalDefault
initialValuebooleanYesfalse

Returns

unknown

Implementation

use-toggle.ts
1import { useState, useCallback } from "react";
2
3export function useToggle(initialValue: boolean = false) {
4 const [value, setValue] = useState(initialValue);
5
6 const toggle = useCallback(() => {
7 setValue((v) => !v);
8 }, []);
9
10 return [value, toggle] as const;
11}

Advertisement

Google Ads

Usage

example.tsx
1import { useToggle } from "@shak-hooks/usehooks";
2
3const result = useToggle(undefined);
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy