美文网首页
React之Refs

React之Refs

作者: PoWerfulHeart | 来源:发表于2018-07-09 20:13 被阅读43次

一.Refs是什么

引用官方的话:
Refs 提供了一种访问在 render 方法中创建的 DOM 节点或 React 元素的方式。

二.为什么需要Refs

在常规的 React 数据流中,props 是父组件与子组件交互的唯一方式。要修改子元素,你需要用新的 props 去重新渲染子元素。然而,在少数情况下,你需要在常规数据流外强制修改子元素。被修改的子元素可以是 React 组件实例,或者是一个 DOM 元素。在这种情况下,React 提供了解决办法。

三.用例

1.第一种用法

String 类型的 Refs

这种使用方法是老版本的用法,当然在16.4.1更新后,明确说了该种方法的Refs存在问题,所以为了不给自己埋坑,建议不要使用这种方式。

class App extends Component{
  constructor(props){
    super(props);
    this.state = {
       value:""
    };
  }

componentDidMount() {
    this.refs.myinput.focus();
}

  render(){
    var str = {
        "width":220,
        "textAlign":"center",
        "margin":"auto"
    }
    return (
       <div style={str}>
          <input type="text" ref="myinput" /> 
       </div>
    )
  }
} 

ReactDOM.render(
    <App />,
    document.getElementById('root')
)

这个例子,当在第一次渲染后,input将会获得焦点。

下面我们改写下例子,来介绍第二种用法:创建 Refs

class Child extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '我是初始值'
    };
  }
  subHandleClick(){
    this.setState({text: '我被父组件调用了,改变了内容!'})
  }
  render(){
    return(
      <div>
        {this.state.text}
      </div>
    )
  }
}

class Parent extends Component {
  constructor(props) {
    super(props);
    this.test = React.createRef();     
  }

  handleClick(){
    console.log(this.test.current);
    this.test.current.subHandleClick();
  }

  render(){
    return(
      <div>
        <input
          type="button"
          value="父组件调用子组件方法"
          onClick={this.handleClick.bind(this)}
        />
        <Child ref={this.test} />
      </div>
    )
  }
}

ReactDOM.render(
  <Parent/>, 
  document.getElementById('root')
);

这里说明一下refs值得类型:
首先ref的值取决于节点的类型:

  • 当 ref 属性被用于一个普通的 HTML 元素时,React.createRef() 将接收底层 DOM 元素作为它的 current 属性以创建 ref
  • 当 ref 属性被用于一个自定义类组件时,ref 对象将接收该组件已挂载的实例作为它的 current 。
  • 你不能在函数式组件上使用 ref 属性,因为它们没有实例。

第三种:回调函数用法

  • React 也支持另一种设置 ref 的方式,称为“回调 ref”,更加细致地控制何时 ref 被设置和解除。
  • 不同于传递 createRef() 创建的 ref 属性,你会传递一个函数。这个函数接受 React 组件的实例或 HTML DOM 元素作为参数,以存储它们并使它们能被其他地方访问。
class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);

    this.textInput = null;

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

    this.focusTextInput = () => {
      // 直接使用原生 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>
    );
  }
}
ReactDOM.render(
    <CustomTextInput/>,
    document.getElementById('root')
)

到这里refs的三种主要用法就介绍完了

相关文章

  • React之Refs

    一.Refs是什么 引用官方的话:Refs 提供了一种访问在 render 方法中创建的 DOM 节点或 Reac...

  • Refs

    Refs提供了一种访问DOM节点或在render方法中创建的React元素的方法。 refs是React组件中非常...

  • 关于react中refs的使用

    在react中,可以使用refs来访问dom,或者在render中创建react对象。 使用refs的主要用途是 ...

  • React 'Refs'

    ref 有两种 ref 方式 ref = ''string" //string ref = {this.saveI...

  • React Refs

    终于看到最后一章了。你可以通过使用 this 来获取当前 React 组件,或使用 ref 来获取组件的引用,实例...

  • React refs

    在一些特殊情况下,直接操作组件实例的方法还是必要或者有利的。所以React提供了一个打破限制的办法,这就是refs...

  • React refs

    1.React.createRef() React.createRef可以给Dom元素添加ref。React.cr...

  • react refs

    以下内容摘自 React文档[https://zh-hans.reactjs.org/docs/refs-and-...

  • React - refs

    Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法中创建的 React 元素。 方式一:...

  • React refs

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

网友评论

      本文标题:React之Refs

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