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

192
Visualizações
How to get return data from another file outside of a react component?

I am trying to get react to return data from a non-component file to a component, I am wondering if that is even possible. If you don't get what I mean here is some code:

foo.js:

import random from "./script/stuff/myfantasticfile.js";

export const myFantasticFunction = () =>{
return "Hello world!";
}

App.jsx:

import react from "react";
import Stuff from "./stuff.jsx"
import {myFantasticFunction} from "./foo.js"

function App(){
const myVariable = myFantasticFunction();
return(
<div>
<Stuff/>
<p>{myVariable}</p>
</div>
)

In other words When the code is executed the p tag will say "undefined" and cannot print "Hello world" on the screen.

I have tried to search on google, stackoverflow and some other online coding websites ie: grepper.

If anyone have any idea please share it to me by posting an answer.

kind regards Alvin.

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

0

Maybe your function is not loaded when it render the component, I suggest to try that:

<p>{myVariable ? myVariable : '' }</p>
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