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

119
Vistas
Is it okay to use jQuery in React only for changing the style (color, border, etc.)?

A lot of people say that it is bad to have both jQuery and React working because they both work on the DOM. But would it be okay if I just used jQuery to change the style properties (e.g. color, border-width, background-color, etc.)? This would be mainly for convenience, as $(.foo) is much easier to use than document.getElementByClassName('foo').

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

0

While possible, it probably wouldn't be a good idea - you'd have to make sure that the style changes get applied an all applicable children when the component rerenders.

If you don't like the verboseness of document.getElementByClassName, feel free to abstract it into a function, eg

const $ = selector => document.querySelector(selector)

But it'd be better to avoid native DOM manipulation methods entirely. In React, the state of the application should flow entirely from React's state.

To change the style in the proper React fashion, write it into the JSX. For example:

const App = () => {
    const [highlighted, setHighlighted] = React.useState();
    return (
      <div
          className={highlighted ? 'highlighted' : ''}
          onClick={() => setHighlighted(true)}
      >click</div>
    );
};

ReactDOM.render(<App />, document.querySelector('.react'));
.highlighted {
  background-color: yellow;
}
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

Or, if you want to set the style directly on the element, rather than using a class:

const App = () => {
    const [highlighted, setHighlighted] = React.useState();
    return (
      <div
          style={{
              backgroundColor: highlighted ? 'yellow' : ''
          }}
          onClick={() => setHighlighted(true)}
      >click</div>
    );
};

ReactDOM.render(<App />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

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