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

190
Views
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 answers
Answer question

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