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

259
Vistas
how can i change my background_image in a div in react js

I do a meteo application and i want to change my background with my city.

my problem is i can change with the classic cssfile like:


body {
  font-family: 'montseratt', sans-serif;
  background-color:white;
  background-image: url('./assets/paris.jpg');
  background-repeat:repeat;

}

but when i retire it and i try in a div like this it doesn't function:

    <div style={{background_image: 'url(./assets/paris.jpg)'}}>

i don't understand why.

There is my entire code but i don't think it's useful

function Accueil()
{

  
  const [query, setQuery] = useState('');
  const [weather, setWeather] = useState({});
  const [time, setTime] = useState(Date.now());
  const [city, setCity] = useState('')
  const [fond, setFond] = useState((''))
  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    const interval = setInterval(() => setTime(new Date().toLocaleString()), 1000);
    return () => {
      
      clearInterval(interval);
    };
  }, []);
  
   const sendtemp = (props) => {
    fetch('getmeteo/?city=' + props) 
    .then((response) => response.json())
    .then((responseData) => {
      responseData.temp -= 273.15
      responseData.temp = responseData.temp.toFixed(2)
      responseData.temp = responseData.temp + "°C"
      setWeather(responseData);
      const pro = props.toLowerCase()
      setFond("./assets/" + pro + ".jpg")
      const pop = pro.charAt(0).toUpperCase() + pro.substring(1)

      setCity(pop)
      setQuery('')
    })
  }
  const search = evt => {
    if (evt.key === "Enter"){
      setQuery('')
      sendtemp(query);

    }
  }

  
  return (
    <div style={{background_image: 'url(./assets/paris.jpg)'}}>

      

<main>
      
        <div className="search-box">
          <input
          type="text"
          className="search-bar"
          placeholder="Search..."
        
          onChange={e =>setQuery(e.target.value)}
          value = {query}
          onKeyPress={search}
          />
          
        </div>
              <div className="location">
              {(time)}
        </div>
        {weather.temp ? (
       <div className="weather-box">
        
       {weather.temp}
         </div>
      ) : ( <div></div>)}
        
        <div className="location">
            {city}
        </div>

        </main>
   
    <div>
      
    </div>
    </div>
    
  
  );
}

thanks for your answers

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

0

** changed response

Looking at the code, there is no reason to have the curly braces, as there is nothing dynamic being rendered. I imagine it should be written the same as a standard HTML element.

 <div style="background_image: url(./assets/paris.jpg)">
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