react-------经过Ref选取dom元素

Ref的做用:Refs 提供了一种方式,容许咱们访问 DOM 节点或在 render 方法中建立的 React 元素。javascript

何时使用:react的状态态的开发形式,能经过状态控制的,应该避免尽量少的使用ref。java

一.建立并访问refs(方法一)react

  建立refsRefs 是使用 React.createRef() 建立的,并经过 ref 属性附加到 React 元素。在构造组件时,一般将 Refs 分配给实例属性,以即可以在整个组件中引用它们。数组

 

  ref 被传递给 render 中的元素时,对该节点的引用能够在 ref  current 属性中被访问ide

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);
    // 建立一个 ref 来存储 textInput 的 DOM 元素
    this.textInput = React.createRef();
    this.focusTextInput = this.focusTextInput.bind(this);
  }

  focusTextInput() {
    // 直接使用原生 API 使 text 输入框得到焦点
    // 注意:咱们经过 "current" 来访问 DOM 节点
    this.textInput.current.focus();
  }

  render() {
    // 告诉 React 咱们想把 <input> ref 关联到
    // 构造器里建立的 `textInput` 上
    return (
      <div>
        <input
          type="text"
          ref={this.textInput} />

        <input
          type="button"
          value="Focus the text input"
          onClick={this.focusTextInput}
        />
      </div>
    );
  }
}

注意:!!ref 的值根据节点的类型而有所不一样:函数

  •  ref 属性用于 HTML 元素时,构造函数中使用 React.createRef() 建立的 ref 接收底层 DOM 元素做为其 current 属性。
  •  ref 属性用于自定义 class 组件时,ref 对象接收组件的挂载实例做为其 current 属性。

 

·         你不能在函数组件上使用 ref 属性,由于他们没有实例this

 

二.使用回调refs来设置refsspa

 React 也支持另外一种设置 refs 的方式,称为回调 refs”。它能助你更精细地控制什么时候 refs 被设置和解除。3d

不一样于传递 createRef() 建立的 ref 属性,你会传递一个函数。这个函数中接受 React 组件实例或 HTML DOM 元素做为参数,以使它们能在其余地方被存储和访问。code

 

下面的例子描述了一个通用的范例:使用 ref 回调函数,在实例的属性中存储对 DOM 节点的引用。

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);

    this.textInput = null;

    this.setTextInputRef = element => {
      this.textInput = element;
    };

    this.focusTextInput = () => {
      // 使用原生 DOM API 使 text 输入框得到焦点
      if (this.textInput) this.textInput.focus();
    };
  }

  componentDidMount() {
    // 组件挂载后,让文本框自动得到焦点
    this.focusTextInput();
  }

  render() {
    // 使用 `ref` 的回调函数将 text 输入框 DOM 节点的引用存储到 React
    // 实例上(好比 this.textInput)
    return (
      <div>
        <input
          type="text"
          ref={this.setTextInputRef}
        />
        <input
          type="button"
          value="Focus the text input"
          onClick={this.focusTextInput}
        />
      </div>
    );
  }
}
View Code

React 将在组件挂载时,会调用 ref 回调函数并传入 DOM 元素,当卸载时调用它并传入 null。在 componentDidMount  componentDidUpdate 触发前,React 会保证 refs 必定是最新的。

 

你能够在组件间传递回调形式的 refs,就像你能够传递经过 React.createRef() 建立的对象 refs 同样

function CustomTextInput(props) {
  return (
    <div>
      <input ref={props.inputRef} />
    </div>
  );
}

class Parent extends React.Component {
  render() {
    return (
      <CustomTextInput
        inputRef={el => this.inputElement = el}
      />
    );
  }
}

在上面的例子中,Parent 把它的 refs 回调函数看成 inputRef props 传递给了 CustomTextInput,并且 CustomTextInput 把相同的函数做为特殊的 ref 属性传递给了 <input>。结果是,在 Parent 中的 this.inputElement 会被设置为与 CustomTextInput 中的 input 元素相对应的 DOM 节点。

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);
    // 建立一个 ref 来存储 textInput 的 DOM 元素
 this.textInput = React.createRef();     this.focusTextInput = this.focusTextInput.bind(this);
  }

  focusTextInput() {
    // 直接使用原生 API 使 text 输入框得到焦点
    // 注意:咱们经过 "current" 来访问 DOM 节点
 this.textInput.current.focus();   }

  render() {
    // 告诉 React 咱们想把 <input> ref 关联到
    // 构造器里建立的 `textInput` 上
    return (
      <div>
<input type="text"  ref={this.textInput} /> <input type="button" value="Focus the text input" onClick={this.focusTextInput} />
</div>
    );
  }
}
相关文章
相关标签/搜索