react的refs

ref是为了获取节点或者子组件的实例
ref在节点上,获取到的是节点元素
ref在classComponent上获取到的是组件实例
ref在functionComponent上是无效的,因为functionComponent没有实例

大部分我们是获取classCompoent实例,但是有时候会获取定义的是functionCompoent,
比如一个PrueComponent就是一个functionCompoent

const TargetComponent=(props)=>{
    <input type="text"/>
}

问题

个人开发者,可以避免在functionComponent上使用ref,如果组件库或者框架开发时,使用者是不清楚,组件库提供的classComponent还是functionComponent

ref是不会跟着props下发下来的

refs目前主要的使用方式有两种

第一种
refs回调(传递给子组件的ref属性一个方法,子组件渲染时候会调用传递下去的方法,方法的参数中带有子组件的元素,这样父组件就拿到了子组件元素的实例)

import React from 'react'

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



class RefsTest extends React.Component {

  constructor(props) {
    super(props)
    this.inputElement = null;
    this.focusTextInput = this.focusTextInput.bind(this)
  }


  focusTextInput = () => {
    this.inputElement.focus();
  };
  // 将ref回调函数定义成class的绑定函数,避免每次渲染都会创建一个新的函数实例
  inputRefFun = (el) => {
    this.inputElement = el
  }

  render() {
    return (
      <div>
        <CustomTextInput
          inputRef={this.inputRefFun}
        />
        <button onClick={this.focusTextInput}>点击</button>
      </div>
    );
  }
}

export default RefsTest;

第二种
使用react提供的forwarRef()方法,配合高阶组件

import React from 'react'

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

function WithRefHOC(Component) {
  class WithRefComponent extends React.Component {
    render() {
      const {
        forwardedRef,
        ...rest
      } = this.props;
      return (
        <Component forwardedRef={forwardedRef} {...rest}></Component>
      )
    }
  }


  return React.forwardRef((props, ref) => {
    console.log(props, ref)
    return <WithRefComponent {...props} forwardedRef={ref}></WithRefComponent>
  })
}

const TextInput = WithRefHOC(CustomTextInput)


const inputRef = React.createRef('b');
function RefsHOCTest() {
  const focusTarget = () => {
    console.log(inputRef.current)
    inputRef.current.focus()
  }
  return (
    <div>
      <TextInput ref={inputRef} name="aa"></TextInput>
      <button onClick={focusTarget}>点击</button>
    </div>
  )
}

export default RefsHOCTest;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值