Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Fetch in React making call continuosly

I am fetching the data from json file but the issue is that when i checked network tab. It's being call continuosly what can i do to resolve the problem. : Network Tab Image

class Banner extends Component {

    constructor(props) {
        super(props);
        this.state = {
          buttonLink:"",
          bannerImg:""
        };
    }


    render() {


        const a =  fetch("./logo.json").then(response => response.json())
        .then((temp1)=> {this.setState({buttonLink:temp1.buttonLink,bannerImg:temp1.bannerImg});  
    });
   
return(....
<img src={this.state.bannerImg}></img>
....
)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

in class component you can use some lifecycle methods like componentDidMount so put your fetch method inside of a componentDidMount not in render method because whenever you call setState() method, it will call render method again,and inside of your render method you call api and setState again, that makes an inifinite loop, do something like this:

 componentDidMount(){
   const a = fetch("./logo.json").then(response => response.json())
  .then((temp1)=> {this.setState({buttonLink:temp1.buttonLink,bannerImg:temp1.bannerImg});  
 }
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't call fetch inside render function.

You can use functional component to easily fetch the data.

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

const Banner = () => {
    const [image, setImage] = useState({ buttonLink: '', bannerImg: '' });

    useEffect(() => {
        fetch('./logo.json')
            .then((response) => response.json())
            .then((temp1) => {
                setImage({ buttonLink: temp1.buttonLink, bannerImg: temp1.bannerImg });
            });
    }, []);

    return <img src={image.bannerImg}></img>;
};

export default Banner;

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda