React中ref的使用

作者: 张培_ | 来源:发表于2018-09-02 22:08 被阅读8次

React中Ref是什么?

ref是React提供的用来操纵React组件实例或者DOM元素的接口。

ref的作用对象

ref可以作用于:

  • React组件的实例
class AutoFocusTextInput extends React.Component {
  constructor(props) {
    super(props);
    this.textInput = React.createRef();
  }

  componentDidMount() {
    this.textInput.current.focusTextInput();
  }

  render() {
    return (
      <CustomTextInput ref={this.textInput} />
    );
  }
}
  • Dom元素
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return <div ref={this.myRef} />;
  }
}

作用于React组件

React组件有两种定义方式:

  • function
    • 对于用function定义的组件是没有办法用ref获取的,原因是: ref回调函数会在组件被挂载之后将组件实例传递给函数。但是使用function定义的函数并没有实例。
    • 但是仍然可以获取function定义的组件中的DOM元素,下面会讲
  • class
    • 用class定义的组件由于可以获取组件实例,因此ref函数会在组件挂载的时候将实例传递给组件

将ref回调函数作用于某一个React组件,此时回调函数会在当前组件被实例化并挂载到页面上才会被调用。

ref回调函数被调用时,会将当前组件的实例作为参数传递给函数。

Parent Component 如何获取Child component中DOM元素?

首先,能够使用ref的child Component必然是一个类,如果要实现,必然要破坏child component的封装性,直接到child component中获取其中DOM。

React16之前的获取方式

破坏封装性的获取方式
  • 定义一个ref回调函数
  • 并将该函数绑定Parent Component的this
  • 将该回调函数传递给子组件
  • 子组件中将该函数赋给需要获取的DOM元素的ref
class App extends Component {
  constructor(props) {
    super(props);
    this.getDOM = this.getDOM.bind(this);
  }

  getDOM(element) {
    this.div = element
  }

  render() {
    return (
      <div>
        <Button getDOM={this.getDOM} />
      </div>
    );
  }
}
//Button.js
export default (props) => (
  <div>
    <button ref={props.getDOM} onClick={props.onClick}>this is a button</button>
  </div>
)

不破坏封装性的获取方式
  • 父组件定义一个ref函数,将ref赋值给Child component
  • Child Component中用ref获取到需要的DOM元素
  • Child Component中定义一个getElement的方法,然后将该DOM元素返回
  • 父组件通过获取Child component再调用getElement即可
//APP.js
class App extends Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
    this.div = React.createRef()
  }

  render() {
    return (
      <div>
        <Button ref={this.div}/>
      </div>
    );
  }
}
//Button.js
import React, {Component} from 'react';

export default class Button extends Component {
  constructor(props) {
    super(props);
    this.button = React.createRef();
    this.getButton = this.getButton.bind(this);
  }

  getButton() {
    return this.button
  }

  render() {
    return (
      <div>
        <button ref={this.button}>this is a button</button>
      </div>
    );
  }
}

React16之后的用Forwarding Refs

Forwarding Refs,React.forwardRef类似一个HOC,参数是一个function,这个function包含两个参数props和ref,返回Component,可以将这个ref用于任何子组件或者DOM

class App extends Component {
  constructor(props) {
    super(props);
    this.div = React.createRef();
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
   ***
  }

  render() {
    return (
      <div>
        <Button ref={this.div} onClick={this.handleClick}/>
      </div>
    );
  }
}

const Button = React.forwardRef((props,ref)=><button 
ref={ref}
>this is a button</button>)
// 此时父组件中的this.div 就是Button中的button dom

注意React.forwardRef参数必须是function,而这个API通常用来解决HOC中丢失ref的问题。

使用ref回调函数的注意点

我们使用ref的时候,正常理解是,ref的回调函数在组件被mount的时候调用一次,将组件的ref赋值个Parent Component的某一个属性,自此之后再不会被重新调用,除非赋了ref的组件被移除。

但是如果使用inline function 作为ref回调函数:

  • 每当数据state、props发生变化
  • render 函数执行,ref inline function就要被重新创建一次,Child Component的ref属性发生了改变
  • React就会将Parent Component的属性值清空,然后再重新赋值
  • 所以inline ref函数就会在每次一render时重新被调用两次

所以尽量避免使用inline function作为Component props

相关文章

  • React学习笔记(三)

    React中的 ref 的使用 ref是一个引用,在React中使用ref来直接获取DOM元素从而操作DOM Re...

  • React 中 ref 的使用

    ref是reference的简写,它是一个引用,在react中,我们使用ref来操作DOM。 在react中,我们...

  • React中ref的使用

    React中Ref是什么? ref是React提供的用来操纵React组件实例或者DOM元素的接口。 ref的作用...

  • React中ref的使用

    在react中,一般情况下,数据通过props来交互,但是在某些情况下,仍需用到ref,在官网中是这样说的 In ...

  • React 中 ref 的使用

    ref 是一个入口 允许您直接访问DOM元素或组件实例。使用ref的三大原则:1.可以在dom元素上面使用ref属...

  • 如何使用React Refs

    如何在React中利用ref以及ref转发,回调ref和HOC React Ref是有用的功能,可作为从父组件中引...

  • React Ref使用

    如何通过refs获得对应的DOM? 使用时通过 this.refs.传入的字符串格式获取对应的元素,使用时 thi...

  • React Ref的使用

    React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上。这个特殊的属性...

  • react自定义组件中使用ref

    一、自定义组件使用ref并且透传子组件ref 自定义组件中使用ref需要用到react的2个hooks:1.for...

  • 组件ref的使用

    自己封装的组件,想使用ref,调用内部的方法1.使用React.forwardRef,包裹组件。 2.接收ref属...

网友评论

    本文标题:React中ref的使用

    本文链接:https://www.haomeiwen.com/subject/bervwftx.html