useToggle
Use boolean value.
API
function useToggle(initialValue?: boolean): [
value: boolean,
actions: {
toggle: (newValue?: boolean) => void;
toggleOn: () => void;
toggleOff: () => void;
}
];
Params:
initialValue: Optional initial value. Default isfalse.
Results:
value: A boolean value that can be changed by the following actions.actions: Actions object.actions.toggle: Toggle thevalue. If thenewValueparam is notundefined, thevaluewill be set to thenewValue.actions.toggleOn: Set thevaluetotrue.actions.toggleOff: Set thevaluetofalse.
Example
Value: true
import React from "react";
import { useToggle } from "@lilib/hooks";
function Example() {
const [value, { toggle, toggleOn, toggleOff }] = useToggle(true);
return (
<>
<button onClick={() => toggle()}>Toggle</button>{" "}
<button onClick={toggleOn}>Toggle on</button>{" "}
<button onClick={toggleOff}>Toggle off</button> {`Value: ${value}`}
</>
);
}
export default Example;