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

149
Visualizações
Reactjs - How to avoid creating a new clickhandler function in each render

In my react component on a button click, i am passing a parameter to my click handler exactly like this

<a
            id={`_primaryAction_${messageObject.id}`}
            href="#"
            class='message'
            onClick={(e: MouseEvent) =>
              this.handleClick(e, messageObject)
            }
          >

I have a usecase where my props are changing and re render is happening . so in each new render this click handler new instance will create. Is there a way to avoid this ?

Edited: removed id and passing wholeObject as it is my use case. Yes this is in loop . This a tag will create for the array of messages.

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

0

First of all, do more research to see if the re-rendering is indeed a cause for concern, as it might not be such a big deal performance-wise.

As a solution, you could create another component which you pass the object.

const ActionLink = (props) => {
  const {
    handleClick,
    messageObject,
    ...otherProps
  } = props;

  const clickHandler = React.useCallback((e: MouseEvent) => {
    handleClick(e, messageObject);
  }, [handleClick, messageObject]);

  return <a 
      {...otherProps}
      onClick={ clickHandler }
    />;
}

export default ActionLink;

And in your case, you can use it like the following (instead of the a)

<ActionLink
  id={`_primaryAction_${messageObject.id}`}
  href="#"
  class="message"
  messageObject={messageObject}
  handleClick={this.handleClick} >...</ActionLink>

And if required, you can further protect against re-renders by passing it through React.memo

export default React.memo(ActionLink);

Lastly as an alternative, you could do as others have suggested and provide the id to an attribute of the link, and use that inside the handleClick method to retrieve the correct message from the list

something like

<a
  id={`_primaryAction_${messageObject.id}`}
  href="#"
  class='message'
  data-message-id={messageObject.id}
  onClick={this.handleClick}
>

and in your handleClick

handleClick(e){
   const messageId = e.target.getAttribute('data-message-id');
   // assuming your message list is named messageList
   // adjust accordingly
   const message = messageList.find(({ id }) => id === messageId);

   // ... do what you were already doing with the message here
}
about 4 years ago · Juan Pablo Isaza Relatório

0

checkout useCallback

useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders

https://reactjs.org/docs/hooks-reference.html#usecallback

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you are using a class component

since you want to pass an object which I think is coming dynamically and not some constant in component (i.e. object is part of a map) and also don’t want to create a new function on every render I would suggest set your button attribute's value as the value of your object and you can access it e.target.value and bind the method than using the inline callback

and it will not create a new function now here's the working example

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