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

193
Views
How to set state from an Axios get request without causing a re-render

I am fairly new to React. I am currently having the problem that I am using the Axios Get request response to update one of my state variables.

However, I am currently facing the problem in which because the state variable is being updated, App gets re-rendered, which then calls the Axios Get request again (causing a sorta infinite loop).

How do I make it so that a Get request can change a variable, but doesn't re-render the component and call a Get request again?

function App(props) {
    let [sentenceData, setSentenceData] = React.useState({})

    const renderEnglishSentence = useCallback(function () {
        return <EnglishSentence sentenceData={sentenceData}/>;
    }, [sentenceData]);


    const fetchSentence = useCallback(function() {
        return axios.get('http://localhost:3030/getrandomunusedsentence')
    })

    fetchSentence().then((response) => 
        setSentenceData(response.data[0])
        return (
            <div>
                {renderEnglishSentence()}
            </div>
        )
    )
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You will want to use the useEffect hook for this. This will only render first time component is loaded.

import React, { useState, useEffect } from 'react';

function App(props) {
    useEffect(() => {
        const fetchSentence = useCallback(function() {
            return axios.get('http://localhost:3030/getrandomunusedsentence')
        })        
    }, []);
}
about 4 years ago · Juan Pablo Isaza Report

0

  const [sentenceData, setSentenceData] = React.useState({})

  React.useEffect(() => {
    axios.get('http://localhost:3030/getrandomunusedsentence')
      .then(() => {
        setSentenceData(response.data[0])
    })
  }, [])

You can make the http request in the useEffect hook with zero dependencies ([])

about 4 years ago · Juan Pablo Isaza Report

0

Api calls should be handled as side effect. You should use useEffect hook for it with no deps, so it will be called once component is mounted.

useEffect(() => {
  const fetch = async () => {
    const response = await axios.get('http://localhost:3030/getrandomunusedsentence');
    const data = await response.json();
    setSentenceData(data);
  }
  
  fetch();
}, []);
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!