addeventlistener in querySelectorAll in Reactjs

Asked

Viewed 17 times

-1

Fala galera

Maybe it’s a simple problem but I’m trying to figure out why it doesn’t work, I researched here in the stack, I found similar questions but they didn’t solve the problem.

I have a dynamically mounted table according to the API

I need to get the TR (parent) ID of the TD element I clicked.

Problem:

  • It only works after the second click
  • After the third click, the item is printed repeatedly on the console

It seems that it is printed the amount of times it has already been clicked.

Tbody

                              <Tbody>
                                    {address.map((index) => {

                                          let tipoPessoa = ''
                                          if (index.tipoPessoa === 1) {
                                                tipoPessoa = 'Física'
                                          }
                                          else {
                                                tipoPessoa = 'Jurídica'
                                          }

                                          return <Tr id={index.id} key={index.id} className='tr'>
                                                <Td>{index.nome}</Td>
                                                <Td>{index.endereco.cidade}</Td>
                                                <Td>{tipoPessoa}</Td>
                                                <Td onClick={detailsAddress}><Icon src={Search} /></Td>
                                          </Tr>
                                    })}
                              </Tbody>

Function to capture ID

      const detailsAddress = () => {
      
            const trs = document.querySelectorAll('.tr')
            trs.forEach((tr)=>{
                  tr.addEventListener('click', ()=>{
                        console.log(tr)
                  })
            })
      }

Console

inserir a descrição da imagem aqui

  • Every time the td is clicked, you are creating and accumulating new addEventListener to the tr.

  • Because the use of querySelectorAll and addEventListener ? If you are in the world React should give preference to useRef and onClick.

1 answer

0

In this case as you have already assigned your function detailsAddress in the event of onClick of his td you can use it to navigate to the parent element, in case the tr, for this just make the following change in its function:

const detailsAddress = (event) => {
      const tdElement = event.target
      const trElement = tdElement.parentElement
      
      console.log(trElement.id)
}

You can simplify things by doing everything in one line:

const detailsAddress = (event) => {
      console.log(event.target.parentElement.id)
}

Another approach would be to pass the id directly to your function:

<Td onClick={() => detailsAddress(index.id)}><Icon src={Search} /></Td>

Then it would be enough to recover that value:

const detailsAddress = (id) => {
   console.log(id)
}
  • Gabriel, thank you for your answer. But anyway, it solved part of the problem, since if the user clicks on the TD the return is the ID, but if clicking on the icon inside the TD is returned Undefined. I believe this may be an application usability problem.

  • If you used the parentElement then really he ends up catching the event.target as the icone, soon the parentElement ends up being the element td and not the tr, I suggest you implement the last example I put in the answer, it’s simpler and you don’t need to be doing element searches in DOM.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.