Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda