Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/useToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { Reducer, useReducer } from 'react';
const toggleReducer = (state: boolean, nextValue?: any) =>
typeof nextValue === 'boolean' ? nextValue : !state;

/**
* useToggle
*
* Un hook sencillo para manejar estados booleanos (on/off).
* Ideal para modales, menús desplegables y checkboxes.
*
* @param initialValue - Valor booleano inicial.
* @returns Un array con el estado actual y una función para alternarlo.
*/

const useToggle = (initialValue: boolean): [boolean, (nextValue?: any) => void] => {
return useReducer<Reducer<boolean, any>>(toggleReducer, initialValue);
};
Expand Down