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

87
Vistas
How to assign props values to inline styles

I'm using create react app and I need to assign props values to a styles of the component. In this case background image 'url' is need to pass as props to styles. I have tried a lot of solutions but none of them worked

I'm making a component named HeroSection with props including backgroundURL and videoURL, I'm then using a statement to assign backgroundImage if the prop videoURL is empty

Home.js

import React from "react";
import HeroSection from "./components/HeroSection";

function Home() {
  return (
    <div className="home">
      <HeroSection
        firstLine="Not just a team,"
        secondLine="We are a "
        autoType={[
          "family of rebels",
          "movement of ideas",
          "collection of culture",
        ]}
        backgroundURL="../assets/hero_pic.jpg"
        videoURL=""
      />
    </div>
  );
}

export default Home;

HeroSection.jsx

import React from "react";
import Typed from "react-typed";

function HeroSection(props) {

  return (
    <div
      className={
        props.videoURL === "" ? "hero-section img-background" : "hero-section"
      }
      style={{
        backgroundImage:
          props.videoURL === "" ? `url(${this.props.backgroundURL})` : "none",
      }}
    >
    
      <h2>
        <span className="first-line">{props.firstLine}</span>
        <br />
        <span className="second-line">{props.secondLine}</span>
        <Typed
          strings={props.autoType}
          typeSpeed={40}
          loop={true}
          backDelay={2000}
          backSpeed={20}
        />
      </h2>
    </div>
  );
}

export default HeroSection;


I HAVE ALSO TRIED THESE STATMENTS AND NONE OF THEM WORKED

    props.videoURL === "" ? `url(${props.backgroundURL})` : "none",
   props.videoURL === "" ? `url('file://${props.backgroundURL}')` : "none",
almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

So the solution was very simple It's due to how webpack handle your assets. Since I pass it just as a string, it can't know that it needs to bundle your png as an asset. In HeroSection I added import backgroundPic from '../assets/hero_pic.jpg' and then used it in the props backgroundURL={backgroundPic} of HeroSection

Thanks for @user3252327 for commenting the solution

almost 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