feat: useBoolean hook (#2314)

* feat: Add useBoolean hook and example usage

* More example usage

* chore: More useBoolean conversion
This commit is contained in:
Tom Moor
2021-07-15 15:27:03 -04:00
committed by GitHub
parent 8884da8a4b
commit 31714efb0b
11 changed files with 87 additions and 73 deletions

23
app/hooks/useBoolean.js Normal file
View File

@@ -0,0 +1,23 @@
// @flow
import * as React from "react";
type InitialState = boolean | (() => boolean);
/**
* React hook to manage booleans
*
* @param initialState the initial boolean state value
*/
export default function useBoolean(initialState: InitialState = false) {
const [value, setValue] = React.useState(initialState);
const setTrue = React.useCallback(() => {
setValue(true);
}, []);
const setFalse = React.useCallback(() => {
setValue(false);
}, []);
return [value, setTrue, setFalse];
}