Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to pass args to event handlers #75

Closed
bvaughn opened this issue Oct 8, 2017 · 10 comments
Closed

Document how to pass args to event handlers #75

bvaughn opened this issue Oct 8, 2017 · 10 comments

Comments

@bvaughn
Copy link
Contributor

bvaughn commented Oct 8, 2017

This issue was originally reported by @StokeMasterJack via facebook/react/issues/9685

A similar issue was reported by @Frazer via facebook/react/issues/8065

Text of original issue #9685

Inside a loop it is common to want to pass a param to an event handler:

<button onClick={(event) => this.deleteRow(23)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this,23)}>Delete Row</button>

That would be a good thing to add to this doc page:

https://facebook.github.io/react/docs/handling-events.html

Also, how to pass args without triggering a re-render in the child component (i.e. creating a new function every time).

@swyxio
Copy link
Contributor

swyxio commented Oct 8, 2017

will work on this

@bvaughn
Copy link
Contributor Author

bvaughn commented Oct 8, 2017

This issue is all yours! 😄

I've added an "in-progress" label so that others will know not to start work on the issue. If you change your mind about the issue, no worries! Just let me know so that I can remove the label and free it up for someone else to claim.

Cheers!

@alexkrolick
Copy link
Collaborator

This topic was also been suggested for the FAQ in #29, so I included some comments in PR #43 (diff).

It might be a good idea to merge the docs on this subject, or link from one to the other.

@Frazer
Copy link

Frazer commented Oct 9, 2017

I think @hedgerh suggestion should also be included.

<button name='23' onClick={this.deleteRow}>Delete Row</button>

deleteRow (e) {
// do some action to (e.target.name)
}

@alexkrolick
Copy link
Collaborator

@Frazer added to the FAQ version of this topic: b2ef5ff

@alexkrolick
Copy link
Collaborator

Closed by #81 I think

@bvaughn bvaughn closed this as completed Oct 18, 2017
@gfortaine
Copy link

gfortaine commented Oct 9, 2019

@StokeMasterJack @bvaughn There is also a third way through currying (see How to pass variables to a function reference?, thx @esthercuan) 👍

deleteRow = id => event => {
    event.preventDefault();
    console.log('row ${id} deleted');
 }

<button onClick={this.deleteRow(23)}>Delete Row</button>

@Frazer
Copy link

Frazer commented Oct 9, 2019 via email

@swyxio

This comment has been minimized.

@bvaughn
Copy link
Contributor Author

bvaughn commented Oct 9, 2019

@Frazer The overhead of creating a function during render is rarely a performance problem. We do it all the time to add event handlers, map arrays, etc. The new hooks API also makes heavy use of this pattern for hooks like useEffect and useMemo (which require creating a function every render, even if it's not called every render).

When it can be a problem is when it breaks memoization and causes a performance sensitive subtree of your application to re-render when it would otherwise not have re-rendered. This is something the DevTools Profiler UI can help you identify and fix though.

jhonmike pushed a commit to jhonmike/reactjs.org that referenced this issue Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants