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

259
Vistas
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 Respuestas
Responde la pregunta

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 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