2

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.

1
  • @GabrielePetrioli Yes this is in loop . I will have array of messages as a prop. i will process that props array and pass it to render element so that i will get array of anchor tags and each has click event Commented Feb 24, 2022 at 7:16

4 Answers 4

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

I see you're using class component. In that case, just move the handler into a separate function.

class MyComponent extends React.Component {
  handleClick = (e) => {
    this.deleteRow(id, e)
  }
  render() {
    return <button onClick={this.handleClick}>Delete Row</button>
  }
}

2 Comments

Here how to catch the id ? if i have object inplace of Id ?
How to get the id ? ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.