React中ref的使用


1.React中Ref是什么?

  ref是React提供的用來操縱React組件實例或者DOM元素的接口。

2.ref的作用對象

  ref可以作用於:

    React組件的實例

 1 class AutoFocusTextInput extends React.Component {
 2   constructor(props) {
 3     super(props);
 4     this.textInput = React.createRef();
 5   }
 6 
 7   componentDidMount() {
 8     this.textInput.current.focusTextInput();
 9   }
10 
11   render() {
12     return (
13       <CustomTextInput ref={this.textInput} />
14     );
15   }
16 }

      Dom元素

1 class MyComponent extends React.Component {
2   constructor(props) {
3     super(props);
4     this.myRef = React.createRef();
5   }
6   render() {
7     return <div ref={this.myRef} />;
8   }
9 }

3.作用於React組件

React組件有兩種定義方式:

  • function
    • 對於用function定義的組件是沒有辦法用ref獲取的,原因是: ref回調函數會在組件被掛載之后將組件實例傳遞給函數。但是使用function定義的函數並沒有實例。
    • 但是仍然可以獲取function定義的組件中的DOM元素,下面會講
  • class
    • 用class定義的組件由於可以獲取組件實例,因此ref函數會在組件掛載的時候將實例傳遞給組件

將ref回調函數作用於某一個React組件,此時回調函數會在當前組件被實例化並掛載到頁面上才會被調用。

ref回調函數被調用時,會將當前組件的實例作為參數傳遞給函數。

4.Parent Component 如何獲取Child component中DOM元素?

首先,能夠使用ref的child Component必然是一個類,如果要實現,必然要破壞child component的封裝性,直接到child component中獲取其中DOM。


5.破壞封裝性的獲取方式
  • 定義一個ref回調函數
  • 並將該函數綁定Parent Component的this
  • 將該回調函數傳遞給子組件
  • 子組件中將該函數賦給需要獲取的DOM元素的ref
     1 class App extends Component {
     2   constructor(props) {
     3     super(props);
     4     this.getDOM = this.getDOM.bind(this);
     5   }
     6 
     7   getDOM(element) {
     8     this.div = element
     9   }
    10 
    11   render() {
    12     return (
    13       <div>
    14         <Button getDOM={this.getDOM} />
    15       </div>
    16     );
    17   }
    18 }
    19 //Button.js
    20 export default (props) => (
    21   <div>
    22     <button ref={props.getDOM} onClick={props.onClick}>this is a button</button>
    23   </div>
    24 )
    6.不破壞封裝性的獲取方式
    • 父組件定義一個ref函數,將ref賦值給Child component
    • Child Component中用ref獲取到需要的DOM元素
    • Child Component中定義一個getElement的方法,然后將該DOM元素返回
    • 父組件通過獲取Child component再調用getElement即可
       1 //APP.js
       2 class App extends Component {
       3   constructor(props) {
       4     super(props);
       5     this.handleClick = this.handleClick.bind(this);
       6     this.div = React.createRef()
       7   }
       8 
       9   render() {
      10     return (
      11       <div>
      12         <Button ref={this.div}/>
      13       </div>
      14     );
      15   }
      16 }
      17 //Button.js
      18 import React, {Component} from 'react';
      19 
      20 export default class Button extends Component {
      21   constructor(props) {
      22     super(props);
      23     this.button = React.createRef();
      24     this.getButton = this.getButton.bind(this);
      25   }
      26 
      27   getButton() {
      28     return this.button
      29   }
      30 
      31   render() {
      32     return (
      33       <div>
      34         <button ref={this.button}>this is a button</button>
      35       </div>
      36     );
      37   }
      38 }

       


免責聲明!

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



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