react 使用 ref 報錯 ,[eslint] Using string literals in ref attributes is deprecated. (react/no-string-refs)


react 項目中給指定元素加事件,使用到 react 的 ref 屬性,Eslink 報錯 [eslint] Using string literals in ref attributes is deprecated. (react/no-string-refs)

常用方法:(會報錯)

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

正確方法:

1 var Hello = createReactClass({
2   componentDidMount: function() {
3     var component = this.hello;
4     // ...do something with component
5   },
6   render() {
7     return <div ref={(c) => { this.hello = c; }}>Hello, world.</div>;
8   }
9 });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM