hedgedoc/frontend/src/hooks/common/use-boolean-state.ts
Tilman Vatteroth e390c0dd15 fix(frontend): reformat source files
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2022-12-04 20:59:46 +01:00

22 lines
794 B
TypeScript

/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useCallback, useState } from 'react'
/**
* Provides a boolean state and two functions that set the boolean to true or false.
*
* @param initialState The initial value of the state
* @return An array containing the state, and two functions that set the state value to true or false.
*/
export const useBooleanState = (
initialState: boolean | (() => boolean) = false
): [state: boolean, setToTrue: () => void, setToFalse: () => void] => {
const [state, setState] = useState(initialState)
const setToFalse = useCallback(() => setState(false), [])
const setToTrue = useCallback(() => setState(true), [])
return [state, setToTrue, setToFalse]
}