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

521
Visualizações
How to stop axios send multiple requests to an api?

I am trying to make an autocomplete input field in my application. I am getting the suggestions from an external API. When I start the app, a lot of calls are sent to the api and they are continuing to be sent until I stop the app. I am not even clicking the button, but the function getSuggestions sends requests. Why is this happening?

import axios from "axios";
import { useEffect, useState } from "react";
import { Button, Form, Row, Col } from "react-bootstrap";
import constants from "../../constants";

function Autocomplete() {
    const [suggestions, setSuggestions] = useState([]);

    function getSuggestions(inputText) {
        const config = {
            params: { search_term: inputText, search_type: 'listings' },
            headers: {
                'x-rapidapi-host': 'zoopla.p.rapidapi.com',
                'x-rapidapi-key': 'x'
            }
        };
        axios.get(`${constants.baseUrl}auto-complete`, config)
             .then(response => {
                 const suggestionsList = response.data.suggestions.map(x => x.value);
                 setSuggestions(suggestionsList);
                 console.log(suggestions);
            })
    }

    useEffect(()=>{
        getSuggestions('Oxford');
    },[]);

    return (
        <Row className="d-flex align-items-center">
            <Col>
                <Form.Control type="text" placeholder="Area" />
            </Col>
            <Col>
                <Button variant="primary" type="button" onClick={getSuggestions('Oxford')}>
                    Search
                </Button>
            </Col>
        </Row>
    );
}
export default Autocomplete;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

on your onClick you need to change (inside of the return)

getSuggestions('Oxford')

to

() => getSuggestions('Oxford')

Otherwise it will run right away on the first render along with your useEffect

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