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

134
Vistas
How to use Props to jQuery in React component

I hope you understand this simple example.

I tried to change the background color of my HTML element on first render by handling it in React Component with a bit help of jQuery.

This is the code inside my React Component (the props is passed from state of Parent Component):

class QuoteBox extends React.Component {
  constructor(props) {
    super(props)
  }
  
  
  componentDidMount() {
    $('#root').css('background-color', this.props.color); 
    $('#text').css('color', this.props.color);
    $('#author').css('color', this.props.color);
  }

  render() {   
    return (
      <div>
          <div id="quote-box" className="quote-box">
            <div className="quote">
              <span id="text" class="quote-text">{this.props.quote}</span>
            </div>
            <div id="author" className="quote-author">
              <span>- {this.props.author}</span>
            </div>
      </div>
    )
  }

The code inside componentDidMount() seem doesn't recognize the this.props.color. But if change to $('#root').css('background-color', 'green'); it's immediately change the background to green on first render.

But in render() it recognize another props.

So what did I do wrong? Is there a way to target HTML element using jQuery inside React?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This most likely happens, because on the first render the props.color is not set. It is most likely, set on the parent element causing a re-render of the QuoteBox, which will not run the componentDidMount which is only run on the first render.

Besides the fact that you should not mix jQuery DOM manipulation with React, just to test your scenario, try using componentDidUpdate instead and it will most likely fix your issue.

about 4 years ago · Juan Pablo Isaza 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