useToggle
React hook to toggle a boolean value.
Add the hook via the CLI:
sh
npx @novajslabs/cli add useToggle
sh
npx @novajslabs/cli add useToggle
sh
pnpm dlx @novajslabs/cli add useToggle
Or copy and paste the code into your project:
ts
import { useState } from "react";
export const useToggle = (initialValue: boolean) => {
const [current, setCurrent] = useState(initialValue);
const handleToggle = () => setCurrent((prev) => !prev);
return { current, handleToggle };
};
js
import { useState } from "react";
export const useToggle = (initialValue) => {
const [current, setCurrent] = useState(initialValue);
const handleToggle = () => setCurrent((prev) => !prev);
return { current, handleToggle };
};
Requirements
React 16.8 or higher
Parameters
initialValue required
Type: boolean
The initial value of the boolean.
Return values
current
Type: boolean
The current value of the boolean.
handleToggle
Type: function
Toggle the boolean value.