React中的Refs

栏目: IOS · Android · 发布时间: 6年前

内容简介:在典型的React数据流中,

在典型的React数据流中, props 是父组件和子组件交互的唯一方式,要修改一个子组件,你需要使用新的props来重新渲染它。但是在某些情况下,你需要在典型的数据流之外强制修改子组件、或获取组件的属性值。被修改的子组件可能是一个React组件的实例,也可能是一个DOM元素。

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

通过React.crateRef()创建

Refs 可以使用 React.createRef() 创建,并通过 ref 属性附加到React元素。在构造组件时,通常将 Refs 分配给实例属性,以便可以在整个组件中调用. ref 作为原生 DOM 的属性传入

class Index extends React.Component {
  constructor(props) {
    super(props);
    this.inputRef = React.createRef();
    this.getInput = this.getInput.bind(this);
  }

  componentDidMount() {
    console.log(this.componentRef.current)
  }

  getInput() {
    console.log(this.inputRef.current.value)
  }

  render() {
    return (
      <div>
        <input ref={this.inputRef}/>
        <button onClick={this.getInput}>getValue</button>
      </div>
    )
  }
}
复制代码

不过此例子并不恰当,在实际运用中能通过状态申明的方式解决,就避免使用 refref 作为组件的属性传入

class IndexPage extends React.PureComponent{
  constructor(props) {
    super(props);
    this.componentRef = React.createRef()
  }

  componentDidMount() {
    console.log(this.componentRef.current)
  }
  
  render() {
    return (
      <div>
        <Ref ref={this.componentRef} />
      </div>
    );
  }
}
复制代码

ref 传递给render中的元素时,对该节点的引用可以通过生成的 Refs 对象的current属性访问。但current属性的值因节点的类型不同而有所不同。

  • ref 作用于html元素时,构造函数中使用 React.createRef() 创建的 ref 接收底层 DOM 元素作为其current属性的值。
  • ref 作用于class申明的 React 组件时,构造函数中使用 React.createRef() 创建的 ref 接收组件实例作为其current属性的值。
  • 函数组件由于没有实例,所以不能给其添加ref属性,系统会警告
    React中的Refs
    React会在组件挂载时,将 DOM 元素或组件的实例传递给current属性,在组件卸载时给current属性值传入 null , ref 会在  componentDidMount  或  componentDidUpdate  生命周期钩子函数触发前更新。

回调函数创建

refs 可以通过回调函数的形式创建,它能助你更精细地控制何时refs被设置和解除。 回调函数接收 DOM 元素或React组件实例作为参数,并将该参数添加到实例属性上,以使它们能在其他地方被存储和访问。回调函数形式创建的 ref ,通过回调函数的参数直接引用不需要通过current。 ref 作为原生 DOM 的属性传入

class Index extends React.Component {
  constructor(props) {
    super(props);
    this.setInputRef = el => {
      this.inputRef = el
    };
    this.getInput = this.getInput.bind(this);
  }
  getInput() {
    console.log(this.inputRef)
  }
  render() {
    return (
      <div>
        <input ref={this.setInputRef} />
        <button onClick={this.getInput}>getValue</button>
      </div>
    )
  }
}
复制代码

ref 作为组件的属性传入

class IndexPage extends React.PureComponent{
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    console.log(this.componentRef)
  }
  
  render() {
    return (
      <div>
        <CallBackRef ref={ el => {this.componentRef = el}} />
      </div>
    );
  }
}
复制代码

ref 作为原生 DOM 的属性传入时回调函数的参数即是该 DOM 对象, ref 作为组件的属性传入时,回调函数的参数即是该React组件的实例对象。React将在组件挂载时,会调用 ref 回调函数并传入对象参数。在 componentDidMount 或 componentDidUpdate 触发前,React 会保证 refs 一定是最新的。

字符串创建

当组件插入到 DOM 后, ref 属性添加一个组件的引用于到 this.refs ,通过 this.refs.xxx 获取对节点的引用

class Index extends React.Component {
  constructor(props) {
    super(props);
    this.getInput = this.getInput.bind(this);
  }
  getInput() {
    console.log(this.refs.inputRef)
  }
  render() {
    return (
      <div>
        <input ref="inputRef" />
        <button onClick={this.getInput}>getValue</button>
      </div>
    )
  }
}
复制代码

在React16.3之后的版本中该方法已经弃用,建议使用前两种。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

这才是马云

这才是马云

陈伟 / 浙江人民出版社 / 2011-5 / 30.00元

“幽默马云”、“开心马云”、“顽皮马云”、“狂妄马云”等。《这才是马云》从各个角度揭开了千面马云的真面目,告诉你一个与想象中大不一样的马云。这不只是一本书,更像一部喜剧电影,让你通过声音、色彩、表情等诸多要素走近马云,感受阿里巴巴。没有冗长的说教,只有让人忍俊不禁的细节;没有高深的理论,只有通俗、诚恳的陈述。作者借幽默平常的琐事,记录下马云“可爱”的一面,看完后让人恍然大悟:原来,马云是这样一个人......一起来看看 《这才是马云》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码