Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda