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

171
Views
How to refactor custom hook that returns values, functions, and *components*?

I have a custom hook that has a bunch of related values and functions that returns a component that requires those values and functions. Currently, I have it in a single hook because it is much cleaner than the alternative.

I've placed random numbers for illustration.

OPTION 1: currently preferred single cleaner hook that returns components

const customHook = ({ dep1, dep2, ...deps}) => {
   // a bunch of related stuff
   return {
      val1,
      ...,
      val9,
      func1,
      ...,
      func5,
      component1: val3 && <Component1 val1={val1} ..... val6={val6} />,
      component2: val5 && <Component2 val1={val1} ..... val6={val6} func1={func1} ... func={func5}/>
   }
}

// component
const MyComponent = () => {
  const { ...vals, component1, component2 }= cusotmHook({ ...deps})

  return (
     <Stuff>
       {component1}
       {component2}
     </Stuff>
  )
}

OPTION 2: alternative to use hook and create components separately

import customHook from './customHook'
import {Component1, Component2} from './Components'
const MyComponent = () => {
  const { vals.... } = customHook({ ...deps })

  return (
     <Stuff>
       <Component1 {...listOfValuesFromHook} />
       <Component2 {...listOfValuesFromHook} />
     </Stuff>
  )
}

I find option 2 to be more cumbersome (obviously simplified for our situation), is this too much of an anti-pattern? Is there a better way to keep it clean without having to pass a large props array to component1, 2, etc.?

about 4 years ago · Juan Pablo Isaza
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!