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

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

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 Relatório

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 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