@spaced-out/ui-design-system
Version:
Sense UI components library
18 lines (12 loc) • 509 B
Flow
// @flow strict
import {useCallback, useState} from 'react';
export function useToggle(
initialState: boolean = false,
): [boolean, (newValue: boolean) => void] {
// Initialize the state
const [stateValue, setStateValue] = useState(initialState);
// Define and memorize toggle function in case we pass down the component,
// This function change the boolean value to it's opposite value
const toggle = useCallback(() => setStateValue((state) => !state), []);
return [stateValue, toggle];
}