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

344
Vistas
React - How to dynamically apply a CSS class to a single table row on hover

I'm trying to highlight the 'Position' and 'Team' cells in pink when any cell in a row is hovered over.

My hover class is being applied, but to the first two cells of every row rather the first two cells of a specific row.

No hover: enter image description here

Hover: enter image description here

The onClick event recognises individual rows (ie I can see the team.teamId when a row is clicked), but the 'tableStyles.rowHovered' class is applied to every row no matter which row I hover over.

const [inHover, setHover] = useState(false);
        
    const hoverStyles = () => {
        
            let styles = `${tableStyles.rowBorderless} ${tableStyles.pointer}`; 
        
            if (inHover) {
              styles += ` ${tableStyles.rowHovered}`;
            }
        
            return styles;
          }
          
          const getTeamRow = (team, index, tableData) => {
        
            let positionShifts = getMinAndMaxPositions(team, tableData);
            //console.log(positionShifts);
        
            return (
              <tr id={'teamRow' + (index + 1)} key={team.teamId} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}>
                <td className={hoverStyles()} onClick={() => { setSelectedTeamId(team.teamId) }}>{index + 1}</td>
                <td className={hoverStyles()} onClick={() => { setSelectedTeamId(team.teamId) }}>{team.teamName}</td>                       
        
                {tablePositionArray.map((td) => {
                  return <td id={'posCol' + td} className={getTableStyles(positionShifts.minPos, positionShifts.maxPos, td)} key={td}></td>                               
                })}                          
              </tr>
            );
          }

This isn't the entire code for my project, just the areas surrounding where my table is formed.

Thank you in advance.

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

0

I can think of 2 options, the problem in your code is that the state of Hover is the state of all the rows.

to solve it you can by at least this 2 options: first and maybe the best : is just add a css to those columns and add the hover behavior for more info look here https://developer.mozilla.org/en-US/docs/Web/CSS/:hover

or : in the hover state you can give index or id of the row to update the style.

    const hoverStyles = (index) => {
        
            let styles = `${tableStyles.rowBorderless} ${tableStyles.pointer}`; 
        
            if (index === inHover) {
              styles += ` ${tableStyles.rowHovered}`;
            }
        
            return styles;
          }

and update the index with index of the row

<tr id={'teamRow' + (index + 1)} key={team.teamId} 
       onMouseEnter={() => setHover(index)} onMouseLeave={() => setHover(null)}>
<td className={hoverStyles(index)} onClick={() => { 
        setSelectedTeamId(team.teamId) }}>{index + 1}</td>
<td className={hoverStyles(index)} onClick={() => { 
        setSelectedTeamId(team.teamId)}}> 

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