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

185
Vistas
How can I determine which checkbox is checked?
<table>
<tbody>
              <tr>  
                <th>Select</th>
                    <th>User Id</th>
                    <th>Id</th>
                    <th>Is Banned?</th>
                    <th>registration_date</th>
                
                </tr>
                {data.map((item, i) => (
                    <tr key={i}>
                        <td><input type="checkbox" checked={isChecked} /></td>
                        <td>{item.user_id}</td>
                        <td>{item.user_name}</td>
                        <td className="isbanned">{item.isbanned}</td>
                        <td>{item.registration_date}</td>
                    </tr>
                ))}
            </tbody></table>

enter image description here

this is my code as you see in up. My question is:

How can I detect user_id 1 checbox is checked?

How should I change my code? or what I can use for that?

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

0

You can do something like this

const data = [
  {
    user_id: "some-id",
    user_name: "Me",
    isbanned: "no",
    registration_date: "2000",
  },
]

function StackOverflow() {
  const [selectedUserIds, setSelectedUserIds] = useState([])

  const addOrRemoveId = (id) => {
    setSelectedUserIds((state) => {
      if (state.includes(id)) {
        // If new id exists in current list then we remove it from state
        return selectedUserIds.filter((currentId) => currentId !== id)
      }
      // Else we add the item id to our state
      return [...state, id]
    })
  }
  return (
    <table>
      <tbody>
        <tr>
          <th>Select</th>
          <th>User Id</th>
          <th>Id</th>
          <th>Is Banned?</th>
          <th>registration_date</th>
        </tr>
        {data.map(({ user_id, ...item }) => (
          <tr key={user_id}>
            <td>
              <input
                checked={selectedUserIds.includes(user_id)}
                onChange={() => addOrRemoveId(user_id)}
                type="checkbox"
              />
            </td>
            <td>{user_id}</td>
            <td>{item.user_name}</td>
            <td className="isbanned">{item.isbanned}</td>
            <td>{item.registration_date}</td>
          </tr>
        ))}
      </tbody>
    </table>
  )
}

To consider: Never use an index or a combination string of them as component keys. Keys are intended to be unique, if you have an userId already, then use it as that key id

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