Passing Arguments to Event Handlers using data-*
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
Currently the documentation https://reactjs.org/docs/handling-events.html mentions two ways how to handle events when the event handler function needs a parameter:
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
As far as I understand, both variants create a new function on each render, which might lead to performance issues because of unnecessary re-renders (you might have to implement custom logic in shouldComponentUpdate). In the documentation:
We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem.
Not mentioned is using a "data'*" HTML attribute and then use that event property in the handler.
I would like to document this variant – or if it is not recommended, document that (and why) this is not recommended.
See also https://github.com/facebook/react/issues/1259
If you consider this worth documenting I will create a PR.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the handling-events documentation page and review the two existing patterns for passing arguments to event handlers, then consider the proposed data-* approach alongside the linked React issue. Done means the page clearly recommends or rejects this variant and explains why.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100