mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 09:25:15 +00:00
34 lines
835 B
JavaScript
34 lines
835 B
JavaScript
|
import { useReducer } from 'react';
|
||
|
/**
|
||
|
* Create a state setter pair for a boolean value that can be "switched".
|
||
|
* Unlike `useState(false)`, `useToggleState` will automatically flip the state
|
||
|
* value when its setter is called with no argument.
|
||
|
*
|
||
|
* @param initialState The initial boolean value
|
||
|
* @returns A tuple of the current state and a setter
|
||
|
*
|
||
|
* ```jsx
|
||
|
* const [show, toggleShow] = useToggleState(false)
|
||
|
*
|
||
|
* return (
|
||
|
* <>
|
||
|
* <button onClick={() => toggleShow()}>
|
||
|
* Toggle
|
||
|
* <button>
|
||
|
*
|
||
|
* {show && <strong>Now you can see me</strong>}
|
||
|
* </>
|
||
|
* )
|
||
|
*
|
||
|
* ```
|
||
|
*/
|
||
|
|
||
|
export default function useToggleState(initialState) {
|
||
|
if (initialState === void 0) {
|
||
|
initialState = false;
|
||
|
}
|
||
|
|
||
|
return useReducer(function (state, action) {
|
||
|
return action == null ? !state : action;
|
||
|
}, initialState);
|
||
|
}
|