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

154
Vistas
How can I update the setDegrees based on the city I write with React?

I'm trying to learn react.

Based on what I write in the input field, the temperature should change consequently, but I can't find which is the best method to do it.

My logic tells me to put the setDegrees under the inputChange function so when I type a new city the weather also change, but I can't do it since the data i'm trying to get aren't global.

import React, {useState, useEffect} from 'react'
import axios from 'axios';
import City from './components/City';


function App() {

  const [city, setCity] = useState("Rome")
  const [degrees, setDegrees] = useState("")

  useEffect(() => {
    
  const options = {
    url: 'https://weatherapi-com.p.rapidapi.com/current.json',
    params: {q: city},
    headers: {
      'X-RapidAPI-Host': 'weatherapi-com.p.rapidapi.com',
      'X-RapidAPI-Key': '---'
    }
  };

  axios.request(options).then(res => {
    setDegrees(res.data.current.temp_c)

   }).catch( error => console.log(error))
  },[])

  const inputChange = e => {
    setCity (e.target.value)
  }

  return (
    <div>
      <input type="text" placeholder='Type the city' onChange={inputChange}/>
      <City 
      city = {city}
      degrees = {degrees}
      />
    </div>
  )
}

export default App
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are missing a logic where you re-fetch results on city change, there are plenty ways to approach it, here is a simple example (lazy fetch):

function App() {
  const [city, setCity] = useState("Rome");
  const [degrees, setDegrees] = useState("");

  const fetchDegrees = (city) => {
    const options = {
      url: "https://weatherapi-com.p.rapidapi.com/current.json",
      params: { q: city },
      headers: {
        "X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com",
        "X-RapidAPI-Key": "---",
      },
    };

    axios
      .request(options)
      .then((res) => {
        setDegrees(res.data.current.temp_c);
      })
      .catch((error) => console.log(error));
  };

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

  const inputChange = (e) => {
    setCity(e.target.value);
  };

  return (
    <div>
      <input type="text" placeholder="Type the city" onChange={inputChange} />
      <button onClick={() => fetchDegrees(city)}>Search city</button>
      <City city={city} degrees={degrees} />
    </div>
  );
}
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