Skip to content

Latest commit

 

History

History
72 lines (57 loc) · 1.7 KB

no-string-refs.md

File metadata and controls

72 lines (57 loc) · 1.7 KB

Disallow using string references (inferno/no-string-refs)

💼 This rule is enabled in the ☑️ recommended config.

Currently, two ways are supported by Inferno to refer to components. The first way, providing a string identifier, is now considered legacy in the official documentation. The documentation now prefers a second method -- referring to components by setting a property on the this object in the reference callback.

Rule Details

Examples of incorrect code for this rule:

var Hello = createClass({
 render: function() {
  return <div ref="hello">Hello, world.</div>;
 }
});
var Hello = createClass({
  componentDidMount: function() {
    var component = this.refs.hello;
    // ...do something with component
  },
  render: function() {
    return <div ref="hello">Hello, world.</div>;
  }
});

Examples of correct code for this rule:

var Hello = createClass({
  componentDidMount: function() {
    var component = this.hello;
    // ...do something with component
  },
  render() {
    return <div ref={(c) => { this.hello = c; }}>Hello, world.</div>;
  }
});

Rule Options

"inferno/no-string-refs": [<enabled>, {"noTemplateLiterals": <boolean>}]

noTemplateLiterals

When set to true, it will give warning when using template literals for refs. Examples of incorrect code for this rule:

var Hello = createClass({
 render: function() {
  return <div ref={`hello`}>Hello, world.</div>;
 }
});
var Hello = createClass({
 render: function() {
  return <div ref={`hello${index}`}>Hello, world.</div>;
 }
});