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

190
Vistas
How can I return text with useState?

I am waiting for the data (when the user submit) i fetch the data then return the Temperature with useState() but i wanted to return a header with it like Temperature:12°C.

Something else is that i wanna round the temperature to 2 decimal places but i don't know how to do so

here is my code:

import axios from 'axios';
import React from 'react'
import {useState, useEffect} from 'react'
import './style.css'
import rain from './use1.png'


  function App() {

    
    const [name,setName] = useState('Cairo')
    const [res, setRes] = useState(null)
    const [pic, setPic] = useState(null)
    const [temp, setTemp] = useState('')


  const getApi = e => {


axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${name}&appid=my_key`)
.then(response=> {
  console.log(response.data);
  setRes(response.data.name)


  setTemp(response.data.main.feels_like-273.15+'°C');
  Math.round(temp)
  setPic(`https://openweathermap.org/img/w/${response.data.weather[0].icon}.png`)

})}

const handleChange = e => {
  setName(e.target.value);
};



  return (
<div>
  <img className="wallpaper" src={rain}></img>
  <div className="content">
    <input placeholder="Search.." className="input" onChange={handleChange}></input>

<a href="#" type="submit" onClick={getApi}><i  id="icon" className="fa fa-search"></i></a>


    </div>

  <div className="content2">
    <h1 className="name">{res}</h1>
    <img src={pic}></img>
    <h1 className="temp">{temp}</h1>
  </div>

</div>
  );
}

export default App;
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Add a useEffect hook so your component re-renders after your temp state changes.

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

In order to round to two decimal places...well, usually I don't like telling people this but that's an extremely easy thing to find out from just using Google or another search engine.

JavaScript math, round to two decimal places

about 4 years ago · Santiago Trujillo Denunciar

0

You can do like this

 const [temp, setTemp] = useState('')
     setTemp(response.data.main.feels_like-273.15+'°C');
        return (
    <div>
        <h1 className="temp"> Temperature {temp?.toFix(2)}</h1>
    </div>
)
about 4 years ago · Santiago Trujillo Denunciar

0

You could do it like:

`Temperature ${(response.data.main.feels_like-273.15).toFixed(2)} °C`
about 4 years ago · Santiago Trujillo 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