Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!