Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
How should I implement this dynamic endpoint HTTP GET request?

I'm currently trying to show the Location depending on the ID number selected in the dropdown. I currently have a handler that will grab the current selected ID in the dropdown. I just have a couple questions.

  1. Should I be doing the endpointLocation GET request in the same useEffect that I grab the ID from endpointID?
  2. How should I handle the default selected dropdown value because event.target.value is undefined until it is changed in onChange. Thus, this makes my dynamic constLocation get a 500 error due to fiddle/getLocation/ + event.target.value

https://jsfiddle.net/Yellohh/wq0trdas/30/

const request = axios.create({
  baseURL: "https://veselka.maka-ars.com",
  timeout: 30000,
});

const endpointIds = "/fiddle/getIds";

function Dropdown() {
  const [ids, setIds] = React.useState([]);
  const [location, setLocation] = React.useState();
  const [value, setValue] = React.useState([]);

  React.useEffect(() => {
    request.get(endpointIds).then((response) => {
      const loadedIds = [];

      for (const id of response.data.ids) {
        loadedIds.push({ id });
      }
      setIds(loadedIds);
    });
  }, []);

  /*  const endpointLocation = "fiddle/getLocation/" + event.target.value; */

  React.useEffect(() => {
    request.get(endpointLocation).then((response) => {
      const loadedLocation = response.data.location;

      setLocation(loadedLocation);
    });
  }, []);

  const valueChangeHandler = (event) => {
    setValue(event.target.value);
  };

  console.log(event.target.value);

  const idsList = ids.map((id) => <option>{id.id}</option>);

  return (
    <div>
      <select onChange={valueChangeHandler}>{idsList}</select>
      <p>{location}</p>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

For your second question, you could give the state an initial value. Perhaps a you have an ID in mind?

You can do this via: (in this case -1 is the initial value)

const [location, setLocation] = React.useState(-1);

You can also validate the request parameters before you execute the request, in this case validate it is defined.

For your first question; are the actions part of the same flow? As in does a certain user action always start a flow that should trigger both actions, or can they trigger independently. If yes then in my opinion they can be grouped together.

Another tip, go checkout the array reducer concept. It can make those for loops to extract values more readable.

about 4 years ago · Juan Pablo Isaza Denunciar

0

To answer your first question in order for the request to be sent each time the user value changes add the value as a dependence inside the useEffect.

And about your second question before you send a request make sure you have a value, if you don't just return

 React.useEffect(() => {
    
      if (!value) return;

      request.get(endpointLocation).then((response) => {
      
      const loadedLocation = response.data.location;

      setLocation(loadedLocation);
              
    });
  }, [value]);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda