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

257
Visualizações
Preventing unnecessary re-renders of table rows / children in React

Suppose there's a table that needs to keep some state, for example, knowing the last element that was hovered over. To do so, we'd need a onMouseOver event on a child element like this:

<tr onMouseOver={handleMouseOver}></tr>

and then the component containing the table would re-render all the children after setState({lastHovered: e.target}) (something along those lines)

If the goal of hover is to make changes to just one row (let's say show/hide something without using CSS) is there a way to keep this state at a higher level than the row and only render changes in the <tr>s that are affected by the hover?

As an example, let's say there's a delay on hovering in or out of the table. When hovering in, children should show a tooltip, but only if hovered directly over. The table/parent need to maintain some state to keep track of this but is there a way to not have to re-render the entire list of children?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For Functional Components

Wrap all the tr elements inside a React.memo (read) to only listen for changes to its internal props. Also, place the onMouseOver event on the table itself and not every child immediate element, then check which child element is in "focus" (i.e., event.target).

Inside table component:

const [lastHovered, setLastHovered] = useState(null);
const [trProps, setTrProps] = useState(null);

return (
  <table onMouseOver={event => setLastHovered(event.target) /* Also check if 'TR' type */ }> 
    <TableRows someData={trProps} />
  </table>
)

Table rows component:

const TableRows = React.memo( ({someData}) => /* Triggers update only if 'someData' changes */
      <> { someData.map( data => <tr>{data}</tr> ) } </>
    )

Note: The above code isn't tested; written to demonstrate a possible solution.

For Class Components

If you're using class components, then create a custom TableRow element and add shouldComponentUpdate() (read) to control rerenders, or use PureComponents (read) instead.

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