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

117
Visualizações
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 Respostas
Responde à pergunta

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 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