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 thenewValue
param is notundefined
, thevalue
will be set to thenewValue
.actions.toggleOn
: Set thevalue
totrue
.actions.toggleOff
: Set thevalue
tofalse
.
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;