Function useBoolean

  • State preconfigured to do everything you could want with a boolean value.

    Example

    const MyComponent = () => {
    const [open, handlers] = useBoolean(false);

    return (
    <>
    <Button onClick={handlers.toggle}>Toggle</Button>
    <Button onClick={handlers.on}>Open</Button>
    <Button onClick={() => { handlers.set(Math.random() < 0.5); }}>Random</Button>
    {open && <Button onClick={handlers.off}>Close</Button>}
    </>
    );
    }

    Parameters

    • initializer: StateInitializer<boolean> = false

      boolean or function that returns a boolean

    Returns readonly [boolean, {
        off: (() => void);
        on: (() => void);
        set: ((value) => void);
        toggle: (() => void);
    }]

    tuple with current state and an object with state setters

Generated using TypeDoc