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

146
Visualizações
How do I add style to a react element within the return function?

I'm trying to add style to an element in my return of a react component, but I want to achieve this without adding a class. My text editor auto fills a style option, but I believe the syntax is wrong, since when trying to add a function on an onClick event, its a little different when its in the return of a react element. For example, instead of onClick=function() its onClick={() => {function()}} I'm hoping that instead of style={"background-color: green;"} its a different syntax to actually allow style changes once it hits the dom.

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

In-line styles can be done, and here is a code example as you have not provided one.

for example, lets inline style an h1 tag

<h1 style={{background-color:'green', color:'white'}}>This is a tilte</h1>

more can be found here

additionally, I would not recommend inline styling as it's not industry-standard and can cause your code to become bloted.

about 4 years ago · Santiago Gelvez Relatório

0

Style tags in react can indeed contain a references to functions.

I am not fully sure if you are working with React component classes or component functions, but your syntax can besides as follows. Create a variable called customStyle which will contain a function that returns your required style object:

customStyle = () => { return { color: 'red' } };

You can then reference customStyle inside markup as follows:

<div style={this.customStyle()}>My Element</div>
about 4 years ago · Santiago Gelvez Relatório

0

idont know if i understood your question well, You can achieve what you want by making a style state, then mutate it whatever style you want with setState

const [style, setStyle] = useState({})

const App = () => {

 return <div style={style}>
 <button onClick={() => setStyle({color: 'red'})}>handler button </button>
 </div>
}
about 4 years ago · Santiago Gelvez 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