mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
639 B
TypeScript
20 lines
639 B
TypeScript
import { useCallback, useState } from "react";
|
|
|
|
// Parameter is the boolean, with default "false" value
|
|
export default function useToggle(initialState = false): [boolean, (nextState?: boolean) => void] {
|
|
// Initialize the state
|
|
const [state, setState] = useState(initialState);
|
|
|
|
// Define and memorize toggler function in case we pass down the comopnent,
|
|
// This function change the boolean value to it's opposite value
|
|
const toggle = useCallback((nextState?: boolean) => {
|
|
if (nextState !== undefined) {
|
|
setState(nextState);
|
|
} else {
|
|
setState((state) => !state);
|
|
}
|
|
}, []);
|
|
|
|
return [state, toggle];
|
|
}
|