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

215
Views
Why am I getting undefined from a string that has data?

So I am pulling data from my database using an API with this code:

let { id } = useParams();
const [Tenant, setTenant] = useState("");
useEffect(() => {
  axios.get(`http://localhost:3001/tenants/byId/${id}`).then((response) => {
    setTenant(response.data);
  });
}, []);

I then try to set a default value on my Material UI textfield using:

<TextField 
    required id="Field1" 
    defaultValue={Tenant.tenantName}
    label="Tenant Name" 
    variant="outlined" 
    onChange={(event) => {setNtenantName(event.target.value)}}
    />

But, I was not getting anything. So I decided to console.log(Tenant.tenantName) and was getting this in my console:

Console Log

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your state is not ready by the time you're trying to access it.

So you can check for it.

{Tenant && (
   <TextField 
        required id="Field1" 
        defaultValue={Tenant.tenantName}
        label="Tenant Name" 
        variant="outlined" 
        onChange={(event) => {setNtenantName(event.target.value)}}
        />
)}

also on a side note it's not common to capitalize your state names.

u can change it to : const [tenant, setTenant] = useState(""); just for convention

about 4 years ago · Juan Pablo Isaza Report

0

What Kevin is proposing should work well, but you can also do something like so, which it might be easier to read and will display a loader while is fetching the data..

if(Tenant === undefined) return <div>Loading data....</div> 
else return (
  <TextField 
    required id="Field1" 
    defaultValue={Tenant.tenantName}
    label="Tenant Name" 
    variant="outlined" 
    onChange={(event) => {setNtenantName(event.target.value)}}
    />
)
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!