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

New rule proposal: Check if this.refs.someRef properties are actually defined #341

Closed
Intellicode opened this issue Dec 1, 2015 · 3 comments
Labels

Comments

@Intellicode
Copy link
Contributor

I had a few occasions myself that some of my components had typo's or missing reference attributes in components causing crashes.

const Hello = React.createClass({
  onFocus() {
    const scrollResponder = this.refs.scrollView.getScrollResponder();
    ...
  }

  render() {
     return <ScrollList />
  }
});
@jimbolla
Copy link

jimbolla commented Dec 1, 2015

Just a heads up... I remember reading (but can't seem to find) a mention that this.refs is eventually going to be phased out by the React team. I believe the reasoning is that ref= is basically an escape hatch that breaks encapsulation and its use should be minimized. To make the code simpler, they'll phase out ref="thing" going into this.refs.thing and instead only support the function-based ref={c => this.thing = c} style. I can't seem to find the github issue where this was discussed, but I remember reading it in the past few months.

A better rule might be to disallow use of this.refs.

@Intellicode
Copy link
Contributor Author

Good point, I'll do some research and if I come across the discussion you mentioned, I'll reference it here.

@Intellicode
Copy link
Contributor Author

"React also supports using a string (instead of a callback) as a ref prop on any component, although this approach is mostly legacy at this point." https://facebook.github.io/react/docs/more-about-refs.html so indeed a rule to prevent using this.refs would make more sense.

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

No branches or pull requests

3 participants