Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
React Hooks: How can I use multiple instances of my useToggle hook on one component?

Overview:

I've created a custom useToggle react hook and I want to use two instances in one component. A toggle for the "Privacy Policy" and a toggle for the "Terms and Conditions."

Question

How can I use the same useToggle hook to get different constants for each useToggle hook such as the following below. A solution for [isToggled, toggle] or { isToggled, toggle } works. Object destructuring is preferred if it is possible.

const privacyPolicy = [isToggled, toggle] = useToggle();
const terms = [isToggled, toggle] = useToggle();


<button title="Privacy Policy" onClick={privacyPolicy.toggle} />
<button title="Terms & Conditions" onClick={terms.toggle} />

useToggle.ts

// Imports: Dependencies
import { useCallback, useState } from 'react';

// Imports: TypeScript Types
import { TReactHookUseToggle } from '@jefelewis/unison-types';

// React Hook: Use Toggle
export const useToggle = (initialState: boolean = false): TReactHookUseToggle => {
  // React Hooks: State
  const [isToggled, setIsToggled] = useState<boolean>(initialState);

  // Toggle
  const toggle: () => void = useCallback((): void => setIsToggled((prevState: boolean) => !prevState), []);

  return [isToggled, toggle];
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To give them different names, do so when you destructure the array:

const [privacyIsToggled, togglePrivacy] = useToggle();
const [termsIsToggled, toggleTerms] = useToggle();

<button title="Privacy Policy" onClick={togglePrivacy} />
<button title="Terms & Conditions" onClick={toggleTerms} />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda