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

436
Views
How to fetch just one time ( stop fetching data after one is fetched )

i am trying to get data from online Api - https://api.randomuser.me/ it get rendom data every time.

import { useState } from 'react';
import './App.css';

function App() {
  var [user, setUser] = useState()
  fetch('https://api.randomuser.me/')
   .then(res => {return res.json()})
   .then(data => setUser(data.results[0].gender)) 

// if i write: .then(data => console.log(data.results[0].gender)) it console log 2 times, i want to fetch data just one time no 2 times

  return (
    <div className="App">
      {user}
    </div>
  );
}

export default App;

display {user} is changing every second, but i want to fetch just one time not every second

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

0

You need to use useEffect, using an empty square brackets as a second parameter to fetch only once:

import { useState, useEffect } from 'react';
import './App.css';

function App() {
  var [user, setUser] = useState()
  
  useEffect(()=>{
    fetch('https://api.randomuser.me/')
    .then(res => {return res.json()})
    .then(data => setUser(data.results[0].gender)) 
  },[])

  return (
    <div className="App">
      {user}
    </div>
  );
}

export default App;
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!