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

212
Visualizações
I want to change the API request based on button click. The number in the API changes and different data loads but component does not rerender

I have a crypto coin API which gives results based on page number. So I want to change the number in the call based on pagination buttons. The number changes but the data does not reload.

const [activePage, setActivePage] = useState(8);
  const getCoinsData = async () => {
    try {
      const response = await Axios.get(
        `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&per_page=100&page=${activePage}&sparkline=true&price_change_percentage=1h%2C24h%2C7d`
      );
      setData(response.data);
      setLoading(false);
    } catch (e) {
      console.log(e);
    }
  };



  useEffect(() => {
    getCoinsData();
  }, []);

This is the pagination structure

<div className="home-pagination">
        <div
          className={
            activePage === 1 ? "pagination-button active" : "pagination-button"
          }
          onClick={() => setActivePage(1)}
        >
          1
        </div>
        <div
          className={
            activePage === 2 ? "pagination-button active" : "pagination-button"
          }
          onClick={() => {
            setActivePage(2);
          }}
        >
          2
        </div>
</div>

How do I re-render the component so the new data loads based on new page number.v

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your state is updating properly n every page click. The only thing you need to do is call the API again. You can handle it in useEffect

  useEffect(() => {
    getCoinsData();
  }, [activePage]);

by adding activePage as a dependency to useEffect, it will call getCoinsData API on every activePage change

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