Skip to content

added documentation for passing arguments to event handlers #81

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

Merged
merged 4 commits into from
Oct 10, 2017

Conversation

swyxio
Copy link
Contributor

@swyxio swyxio commented Oct 8, 2017

hi, addressing issue #75 !

image

@reactjs-bot
Copy link

reactjs-bot commented Oct 8, 2017

Deploy preview ready!

Built with commit 7f8d084

https://deploy-preview-81--reactjs.netlify.com

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove package-lock.json from this PR 😄

@swyxio swyxio force-pushed the documentArgsIssue75 branch from 059bd0c to 6dad6d2 Compare October 9, 2017 02:47
@swyxio
Copy link
Contributor Author

swyxio commented Oct 9, 2017

sorry bout that. package-lock never used to matter when i was just working on my little repos. removed and recommitted.

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

or alternatively (especially if you want to avoid triggering a re-render in a child component):
Copy link
Contributor

@bvaughn bvaughn Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line isn't actually true ^

function foo() {}
const ctx = {};
const a = foo.bind(ctx);
const b = foo.bind(ctx);
console.log(a == b); // false

Binding in the constructor is a way to avoid unnecessary updates but that's not an option when you want to pass in-scope variables like this.

That being said, I think we should still show the .bind() form for people who prefer to write vanilla ES5 but it doesn't perform differently in terms of shouldComponentUpdate (at least not that I'm aware of).

One way to avoid unnecessary renders would be:

class Parent extends Component {
  // This property will be initialized once during instantiation
  // Subsequent renders will use the same function
  deleteRow = (index) => {};

  render() {
    // Render stuff, including:
    <DeleteRowButton index={i} deleteRow={this.deleteRow} />
  }
}

class DeleteRowButton extends Component {
  // This property will be initialized once during instantiation
  // Subsequent renders will use the same function
  onClick = () => {
    this.props.deleteRow(this.props.index);
  };

  render() {
    return <button onClick={this.onClick}>Delete Row</button>;
  }
}

But all of this ^ is overkill for a simple <button>. It would only make sense to do if the component you're rendering is expensive (eg has as lot of nested children or complex logic).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the examples conflate the method of passing params to events (where this pattern is valid) with passing params within loops (where this pattern is not valid as you point out). I should be clearer there AND also provide the hint to pass by props as newbies can overlook that simple solution. I agree the rerendering thing is overkill and if the reader is worrying about perf to that extent they probably wouldnt be reading this section of the docs anyway. Was only including it because it was suggested in the issue raised.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments about how and when to avoid binds to the FAQ page (#43). It's probably OK to keep it high-level here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably OK to keep it high-level here.

Agreed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsimony is a virtue when it comes to docs. I will just delete the problematic line of code so as not to mislead/confuse.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your thinking, but I actually disagree with the decision you guys ended up making. I came to this thread specifically because I was confused why there is a warning about re-renders for onClick={(e) => this.handleClick(e)} but not for onClick={(e) => this.deleteRow(id, e)}.

Also, being that I'm a relative newbie to React, I didn't fully understand the ramifications of these potential re-renders. The discussion here actually helped me get a sense of how serious a problem they might be from a performance standpoint.

I understand not wanting to pollute the documentation with too much detail, but it would be nice to put the extra details in something akin to footnotes.

@swyxio
Copy link
Contributor Author

swyxio commented Oct 10, 2017

Pushed and awaiting your review. sorry for the original problematic line (i'm still not super comfortable when it comes to questions of context)

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a bunch for the changes 😄

@bvaughn bvaughn merged commit e9a67af into reactjs:master Oct 10, 2017
@gaearon
Copy link
Member

gaearon commented Oct 10, 2017

Tweaked in db1262d.

"Param" is not explained, and people might confuse it with some React term. "Parameter" is more explicit.

Also added reference links.

Finally, these two lines weren't equivalent, and people would likely ask how to get access to the event. So I made them consistent.

Thanks for working on this change!

@bvaughn
Copy link
Contributor

bvaughn commented Oct 10, 2017

Nice changes Dan. Thanks for adding to the section.

@swyxio swyxio deleted the documentArgsIssue75 branch October 10, 2017 22:35
jhonmike pushed a commit to jhonmike/reactjs.org that referenced this pull request 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

Successfully merging this pull request may close these issues.

7 participants