Skip to content

No moar unbound event handlers - #637

Merged
joshaber merged 50 commits into
masterfrom
react-this-binding-issue
Nov 16, 2016
Merged

joshaber merged 50 commits into
masterfrom
react-this-binding-issue

Conversation

@niik

@niik niik commented Nov 15, 2016 •

Copy link
Copy Markdown
Member

This turns on the react-this-binding-issue rule see #590

Several errors can occur when using React and React.Component subclasses. When using React components you must be careful to correctly bind the 'this' reference on any methods that you pass off to child components as callbacks. For example, it is common to define a private method called 'onClick' and then specify onClick={this.onClick} as a JSX attribute. If you do this then the 'this' reference will be undefined when your private method is invoked.

The React documentation suggests that you bind the 'this' reference on all of your methods within the constructor: this.onClick = this.onClick.bind(this);. This rule will create a violation if

  1. a method reference is passed to a JSX attribute without being bound in the constructor. And
  2. a method is bound multiple times in the constructor.

Another issue that can occur is binding the 'this' reference to a function within the render() method. For example, many people will create an anonymous lambda within the JSX attribute to avoid the 'this' binding issue: onClick={() => { this.onClick(); }}. The problem with this is that a new instance of an anonymous function is created every time render() is invoked. When React compares virutal DOM properties within shouldComponentUpdate() then the onClick property will look like a new property and force a re-render.

You should avoid this pattern because creating function instances within render methods breaks any logic within shouldComponentUpdate() methods. This rule creates violations if

  1. an anonymous function is passed as a JSX attribute. And
  2. if a function instantiated in local scope is passed as a JSX attribute.

This rule can be configured via the "allow-anonymous-listeners" parameter. If you want to suppress violations for the anonymous listener scenarios then configure that rule like this: "react-this-binding-issue": [ true, { 'allow-anonymous-listeners': true } ]

The code changes are mostly mechanical with the notable exception of the branches (7cb42b2) and repositories (affbad3) lists where I had to move computation of the grouped lists into their individual state bags.

I also had to create a new component for toolbar items (fd26897) in order to have a place to store state.

I found an error in our Checkbox component, it never updated its state when being re-rendered in place. With our previous model that never happened, it was always recreated when rendering a list. Now it gets re-rendered so we handle receiving new props and update the DOM accordingly (46bad1c)

Fixes #590

@niik niik changed the title [WIP] No moar unbound event handlers \No moar unbound event handlers Nov 16, 2016
@niik niik changed the title \No moar unbound event handlers No moar unbound event handlers Nov 16, 2016
@niik

niik commented Nov 16, 2016

Copy link
Copy Markdown
Member Author

This is ready for review 🙇

@joshaber

Copy link
Copy Markdown
Contributor

#jolo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants