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

100
Views
How to decide when to rerender in React

I'm creating a small project with the mern stack. In the homepage of this project, you can see the things that the current user has. THe problem is that the list can change in every moment, because other user can happend to that list other stuff. SO, to make it refresh, I put it in the useEffect hook. The problem now is that my server is reciving a tons of request, and always the same one. I don't know if is the case to set a timer that rerender after x second, to make the work of my server lighter, or there is a way to remake the request to the server in some case. Here is the small code that manage the request:

    const [file, setFile] = useState([]);
    useEffect(() => {
        axios.post("http://127.0.0.1:5050/file/getfilesbycreator",{creator:localStorage.getItem('user')})
        .then(res => {
            setFile(res.data);
        })
        .catch(err => console.log(err))
    })

If someone has any suggestion, please tell me. I read something about rerender react, but I didn't found out better way then a timer, or something similar.Thanks

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

0

As the second parameter of your useEffect, you have to indicate which variable you are looking for to rerender. If you want the useEffect to render only one time, you have to put [] as second parameter like that :

useEffect(() => {
    axios.post("http://127.0.0.1:5050/file/getfilesbycreator",{creator:localStorage.getItem('user')})
    .then(res => {
        setFile(res.data);
    })
    .catch(err => console.log(err))
}, [])

If you have a variable that can change, for example 'file', just add it in the array :

useEffect(() => {
    axios.post("http://127.0.0.1:5050/file/getfilesbycreator",{creator:localStorage.getItem('user')})
    .then(res => {
        setFile(res.data);
    })
    .catch(err => console.log(err))
}, [file])
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!