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

123
Views
Change the background in a few hours

Translation results I want to change the background image at specified times and I wrote my code as follows

  return (
  <div className="degree-box" style={
                {
                    backgroundImage: time2 > 19 ? `url(${Night})` ||
                        backgroundImage : time2 > 16 ? `url(${Afternoon})` ||
                            backgroundImage : time2 > 7 ? `url(${Morning})` : null
                }} >
       //code...         
                
  </div>
);

But it gives me back an error that the background does not recognize the second and third images. How can I solve this problem?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do it by using nested ternary operations.

return (
  <div className="degree-box" style={
                {
                    backgroundImage: time2 > 19 ? `url(${Night})` :
                        ( time2 > 16 ? `url(${Afternoon})` : (
                            time2 > 7 ? `url(${Morning})` : null ))
                }} >
       //code...
   
  </div>
);
about 4 years ago · Juan Pablo Isaza Report

0

You don't need to overcomplicate things. As you can see even StackOverflow can't highlight your code as expected, because you already have your property name defined, all you need to supply is a value.

{
    backgroundImage: time2 > 19 ? `url(${Night})` :
        time2 > 16 ? `url(${Afternoon})` :
        time2 > 7 ? `url(${Morning})` : null
}

Even shorter to avoid repeating yourself;

{
    backgroundImage: `url(${time2 > 19 ? Night : time2 > 16 ? Afternoon : time2 > 7 ? Morning:null})`
}
about 4 years ago · Juan Pablo Isaza 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!