Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

92
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda