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

140
Visualizações
React setState isn't setting state inside useEffect

I'm pretty new to hooks and I'm trying to use setState for data I'm getting back from an API, but the state never updates. I only need to make the call when the component mounts, which is why the second argument in my useEffect is an empty array. I can see that I'm getting back the data when I console.log the response, it just isn't being set.

const [routeOptions, setRouteOptions] = useState()

useEffect(() => {
    Axios.get("https://svc.metrotransit.org/NexTrip/Routes?format=json").then(response => {
        const routes = response.data
        setRouteOptions(routes)
    });
}, []);

I then try to map through the data like so:

{routeOptions && routeOptions.map(option => {
                <option>
                    {option.description}
                </option>
            })}

but because my state never got set there's nothing to map through.

I may be missing something super obvious cause I'm not familiar with hooks, so any help is appreciated!

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

0

You need to return a value from your .map(). Make sure to give your <option> a unique key prop as well.

Note also that the route property Description has a capital D :)

    <select>
      {routeOptions.map((option) => {
        return <option key={option.Route}>{option.Description}</option>;
      })}
    </select>

here it is all together

import { useState, useEffect } from "react";
import Axios from "axios";

export default function Test() {
  const [routeOptions, setRouteOptions] = useState(null);

  useEffect(() => {
    Axios.get("https://svc.metrotransit.org/NexTrip/Routes?format=json").then(
      (response) => {
        const routes = response.data;
        setRouteOptions(routes);
      }
    );
  }, []);

  if (!routeOptions)
    return (
      <div>
        <p>Loading...</p>
      </div>
    );

  return (
    <select>
      {routeOptions.map((option) => {
        return <option key={option.Route}>{option.Description}</option>;
      })}
    </select>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

try to use Async-select https://react-select.com/async and make a function that returns an array of pair {label, value} instead of wasting your time stuck here.

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