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

257
Views
How to export functions from a custom hook

The basic structure of my hook:


const useMyHook = () => {
  const func1 = (): boolean => { ... }
  const func2 = (type: "type1" | "type2"): boolean => { ... }
  const func3 = (): boolean => { ... }

  return {
    func1, func2, func3
  }
}

export default useMyHook

I'm currently using it like this, which works in dev, but breaks my build.

import useMyHook from "../hooks/useMyHook";

const { func1 } = useMyHook()

const handleSubmit = (e: React.FormEvent) => {
  e.preventDefault();
  const isValid = func1();

  if (isValid) {
  // do more things
  }
}

return (
  <form>...</form>
)

When running yarn build, I am faced with this error:

Objects are not valid as a React child (found: object with keys {func1, func2, func3}). If you meant to render a collection of children, use an array instead.

My attempt at solving this was to export my custom hook functions in an array, as the error seemed to suggest.

  return [
    func1, func2, func3
  ]

And using it like this:

  const [ func1 ] = useMyHook();
  const isValid = func1();

TypeScript is not happy with this:

An argument for 'type' was not provided.

But this is confusing, because func1 does not accept a type argument as func2 does.

Questions

  1. Is there some way I work around this build-time error and export the { } of functions? This feels like the cleanest approach.

  2. If there is no way to achieve 1, how can I properly use the exported array of functions without my types being misread?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There's no issue in returning an object from a hook.

The issue is somewhere inside a render, you are rendering the hook results. You did not post the rendering part of your component.

Also, usually custom hooks are built on top of other hooks. If you don't use any other hooks inside your custom hook, it's not really a hook, it's just a function (maybe prefix with use only if you really define a hook on top of other hooks)

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!