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

168
Visualizações
Call a function imported from another file

I have a function that I declared on a file, like this

export default function doSomething (param1, param2, param3) {
 *Do something here*
}

And then I want to use that function whenever I press a button on a screen I have declared in another file:

import doSomething from '../../doSomething';

export default function Screen(props) {
 return (
   <Container>
      <SafeAreaProvider>
        <Button onPress={() => doSomething(1, 2, 3)} />
      </SafeAreaProvider>
   </Container>
 );
}

However, any time I press the button, it gives me a Invalid hook call. Hooks can only be called inside of the body of a function component error. I'm fairly new to custom hooks/external functions, so how can I resolve is?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If doSomething is a hook and not a plain JS function, then you can not just import it and call it as you would do with plain functions.

The following code fixes the issue.

export default function useDoSomething() {

   ...

  const doSomething = React.useCallback((parameter1, parameter2, parameter3) => {}, [])
  return {
    doSomething,
  }
}

Then use the hook as follows.

const { doSomething } = useDoSomething()

return (
   <Container>
      <SafeAreaProvider>
        <Button onPress={() => doSomething(1, 2, 3)} />
      </SafeAreaProvider>
   </Container>
);
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