为什么不在ReactJs中使用当前属性的自有对象,而不是创建Ref?

huangapple go评论85阅读模式
英文:

Why not use own object with current property rather than createRef in ReactJs

问题

ReactJs文档中提到:

> 使用 React.createRef() 创建并通过ref属性附加到React元素的引用

记录ref会得到一个具有current属性设置为null的对象。出于好奇,我创建了一个具有current属性的普通对象,并尝试将其用作ref。以下是测试用例,它居然可以工作:

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = {}; //可以工作!!!
    //console.log(React.createRef());
    //绑定
    this.RefFocus = this.Myfocus.bind(this);
  }
  componentDidMount() {
    console.log(this.myRef.current);
  }
  Myfocus() {
    this.myRef.current.focus();
  }

  render() {
    return (
      <div>
        <input
          type="text"
          ref={this.myRef} />
        <input
          type="button"
          value="Focus the text input"
          onClick={this.RefFocus}
        />
      </div>
    );
  }
}

//const app = ReactDOM.createRoot(document.getElementById("app"));
ReactDOM.render(<CustomTextInput />, document.getElementById("app"));

因此,问题是,我们为什么需要createRef API?React是否对createRef有特殊处理,例如更好的性能、内存优化等,或者是否存在需要ref转发的情况,普通的{current: null}不能工作?

请用简单的语言解释一下,我是React和JavaScript的新手。

英文:

In ReactJs documentation, it is said

> Refs are created using React.createRef() and attached to React elements via the ref attribute

Logging the ref gives an object which has a current property set to null. Just out of curiosity, I created a normal object with current property and tried to use it as a ref. Following is the test case and it somehow works:

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);    
    this.myRef = {}; //Works!!!
    //console.log(React.createRef());
    //bind
    this.RefFocus = this.Myfocus.bind(this);
  }
  componentDidMount() {
    console.log(this.myRef.current);
  }
  Myfocus() {
    this.myRef.current.focus();
  }


  render() {
    return (
      &lt;div&gt;        
        &lt;input
          type=&quot;text&quot;
          ref={this.myRef} /&gt;
        &lt;input
          type = &quot;button&quot;
          value = &quot;Focus the text input&quot;
          onClick = {this.RefFocus}
        /&gt;        
      &lt;/div&gt;
    );
  }
}

//const app = ReactDOM.createRoot(document.getElementById(&quot;app&quot;));
ReactDOM.render(&lt;CustomTextInput /&gt;, document.getElementById(&quot;app&quot;));

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

So the question is, why do we need the createRef api at all? Is there something special that React does with createRef, e.g. better performance, memory optimization etc or are there cases like ref forwarding where a custom {current: null} wouldn't work?

Please explain in easy language, I am new to React and JavaScript.

答案1

得分: 0

CreateRef 只是一个包装器,用于简化编写和阅读代码。

createRef 的实现如下(React 源代码):

export function createRef() {
  const refObject = {
    current: null,
  };
  return refObject;
}
英文:

CreateRef is just a wrapper to simplify writing and reading code

The createRef implementation looks like this (source code react):

export function createRef() {
  const refObject = {
    current: null,
  };
  return refObject;
}

huangapple
  • 本文由 发表于 2023年6月19日 21:33:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定