Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!