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

212
Vistas
Should I use event delegation, to be economical with JS-events at i.e. table cell level?

If I have a table in which I want to react to table-cell events, is it better to put the event on the table-element and handle cell specific events with the help of i.e. closest("td"), or is it ok to put the event on the table cell? (I'm talking about tables with a large number of cells, all of which are visible.)

// pseudo react-code
return (
  <table onClick={handleCellClick}>
    <tr>
      <td>...</td><td>...</td><td>...</td><td>...</td>
    </tr>
    //...
  </table>
)

or

// pseudo react-code
return (
  <table>
    <tr>
      <td onClick={...}>...</td><td onClick={...}>...</td><td onClick={...}>...</td><td onClick={...}>...</td>
    </tr>
    //...
  </table>
)
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Since you're asking about React specifically: No, you don't need to put only a single event handler at the table, React already does event delegation for you. So using <td onClick={…}> is probably a lot simpler, especially if you use nested components.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Without really knowing what your click handler is meant to be doing, here's an an example using event delegation, and data attributes, to show how you can identify individual cells.

const { useState } = React;

function Example() {

  function handleClick(e) {
    if (e.target.matches('td')) {
      const { dataset: { id } } = e.target;
      console.log(id);
    }
  }

  return (
    <table onClick={handleClick}>
      <tr>
        <td data-id="1-1">1-1</td>
        <td data-id="1-2">1-2</td>
        <td data-id="1-3">1-3</td>
        <td data-id="1-4">1-4</td>
      </tr>
      <tr>
        <td data-id="2-1">2-1</td>
        <td data-id="2-2">2-2</td>
        <td data-id="2-3">2-3</td>
        <td data-id="2-4">2-4</td>
      </tr>
    </table>
  );

}

ReactDOM.render(
  <Example />,
  document.getElementById('react')
);
table { border-collapse: collapse; bordeR: 1px solid #565656; }
td { padding: 0.44em; border: 1px solid #dfdfdf; }
td:hover { cursor: pointer; background-color: #ffff00; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Conclusion is that, in vanilla JavaScript, event delegation is the preferred solution, to avoid memory and performance issues. If you are using a JavaScript frontend library like React.js or a framework like Ember, Angular, ... you have to read the docs and check how events are managed. In case of React.js it is unnecessary, to implement custom event delegation, because React.js does this optimisation for you.

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