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

148
Vistas
How do I use a fetch get response more than once?

I want to set both of these values in the state to be used later as variables.

  const [dollarGoal, setDollarGoal] = React.useState(0); 
  const [dollarValue, setDollarValue] = React.useState(0);
  //fetching donation value
  fetch('api', {
      method: 'GET',
    })
    .then(response => response.json())
    .then(r => setDollarValue(r.dollarValue))
    .then(r => setDollarGoal(r.dollarGoal));

But it gives the error:

Property 'dollarGoal' does not exist on type 'void'.

How do I fix this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this:

  fetch('api', {
      method: 'GET',
    })
    .then(response => response.json())
    .then((r) => {
      setDollarValue(r.dollarValue)
      setDollarGoal(r.dollarGoal)
    });

The last two .then statements need to be combined. By calling .then twice, you're sending the result of setDollarValue() (a void function) to the next .then, which is not what you want.

about 4 years ago · Juan Pablo Isaza Denunciar

0

First off i think you should place your api call inside of a useEffect hook, because if not the fetch will be triggered again right after you set the values of your response with your useState hook, it might lead to an infinite call when it will be working. Then, i think your response might not contain the property named 'dollarValue'. Have you tried console.log your response ?

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