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

174
Visualizações
react jsx inline style not rendering when populated from a variable

I added an inline style element to a div tag in a react component along with a className element. The className property renders but the style is not displaying. I am setting from an object variable. However, if I set the style element directly using double curly braces it works, just not working from a variable.

Here is an image of the object value I am using to set the style element.

enter image description here

This shows where the style element should be getting set as the component is rendering, the styleElement object has a value set at this point

enter image description here

This shows the component post rendering and the style element is not present

enter image description here

What would prevent the style element from rendering even though is it populated from an object containing CSS properties?

over 4 years ago · Santiago Trujillo
6 Respostas
Responde à pergunta

0

use ES6 for this.

background: `url(${imageUrl}) no-repeat`
over 4 years ago · Santiago Trujillo Relatório

0

Add " surrounding the imageURL

{ background: "url(\"" + imageUrl + "\") no-repeat ..."}
over 4 years ago · Santiago Trujillo Relatório

0

It seems like your issue lies in the !important part. React doesn't support using !important in the style object. If you can get rid of that !important part it would be for the best, but if you can't then here is a workaround (taken from this SO answer):

<div
  ref={(node) => {
    if (node) {
      node.style.setProperty('background', `url(${imageUrl}) no-repeat 50% 40%`, 'important');
    }
  }}
/>
over 4 years ago · Santiago Trujillo Relatório

0

I believe that !important is the main culprit here. Simply remove it and everything should render with the variable inside.

Inline CSS will, as a general rule, override any CSS in your stylesheet assuming the same level of specificity.

Have a look at MDN for more info about specificity and !important to learn more

over 4 years ago · Santiago Trujillo Relatório

0

the background image syntax is

background-image: url("img_tree.gif");

You are missing the " on between the file name. change to

{ background: 'url("' + imageUrl + '") no-repeat 50% 40% !important' }

the other thing you might want to check is make sure img_tree.gif is actually the correct path, usually, it should relative path or absolute path, something like ./img_tree.gif or http://example.com/img_tree.gif

also make sure have a background size

background-size: cover 
over 4 years ago · Santiago Trujillo Relatório

0

A rule about !important

  • Any values in an element’s style property take precedence over anything inherited by a style sheet (even if the style sheet rule uses the !important annotation). So you do not have to use !important inside inline styling

I think you might need to import the path in jsx

// require also works
background:`url(${import("./images/your_image")}) no-repeat 50% 40%`

If it does not work, try this:

 style={{
      backgroundImage: `url(${require('./images/your_image')})`,
      backgroundRepeat: 'no-repeat',
    }}
over 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