react/no-string-refs 正确性
其作用
此规则禁止在 ref 属性中使用字符串文字。
示例
本规则对于不正确的代码示例
jsx
var Hello = createReactClass({
render: function () {
return <div ref="hello">Hello, world.</div>;
},
});
var Hello = createReactClass({
componentDidMount: function () {
var component = this.refs.hello;
// ...do something with component
},
render: function () {
return <div ref="hello">Hello, world.</div>;
},
});
本规则对于正确的代码示例
jsx
var Hello = createReactClass({
componentDidMount: function () {
var component = this.hello;
// ...do something with component
},
render() {
return (
<div
ref={(c) => {
this.hello = c;
}}
>
Hello, world.
</div>
);
},
});