React-refs

文章介绍了在React中使用ref的三种方式:1)字符串形式的ref,不推荐使用,因为效率不高;2)回调形式的ref,虽然在更新时可能渲染两次,但影响可忽略;3)官方推荐的createRef方法。每种方式都展示了如何获取DOM元素并使用它们。
摘要由CSDN通过智能技术生成

1、字符串形式的ref不推荐使用(效率不高)

 class GetInputValue extends React.Component {
      getInput1Vaule=()=>{
        console.log(this) /*this是实例本身,有refs属性*/
        console.log(this.refs.input1.value)/*通过ref获取的是真是DOM*/
      }
      getInput2Value=()=>{
        console.log(this.refs.input2.value)
      }
      render() {
        return (
          <div>
            <input ref="input1"></input>
            <button onClick={this.getInput1Vaule}>获取第一个input值</button>
            <input ref="input2" onBlur={this.getInput2Value}></input>
          </div>
        )
      }
    }
    ReactDOM.render(<GetInputValue />, document.getElementById('test'))

2、回调形式的ref(放在实例自身上,内联函数绑定ref在更新的时候会渲染两次,但是影响忽略不计)

 class GetInputValue extends React.Component {
      getInput1Vaule = () => {
        console.log(this) /*this是实例本身,有refs属性*/
        console.log(this.input1.value)/*通过ref获取的是真是DOM*/
      }
      getInput2Value = () => {
        console.log(this.input2.value)
      }
      render() {
        const { name, age, sex } = this.props /*接收传递过来的参数*/
        return (
          <div>
            <input ref={currentNode => { console.log(currentNode, this,'currentNode'); this.input1 = currentNode }}></input>
            <button onClick={this.getInput1Vaule}>获取第一个input值</button>
            <input ref={currentNode => this.input2 = currentNode} onBlur={this.getInput2Value}></input>
          </div>
        )
      }
    }
    ReactDOM.render(<GetInputValue />, document.getElementById('test'))

在这里插入图片描述
3、通过createRef进行绑定(官方最推荐)

class GetInputValue extends React.Component {
      input1 = React.createRef() /*每绑定一个都要重新定义一个箭头函数挂载到实例上,要不后面定义的会覆盖前面的*/
      input2 = React.createRef()
      getInput1Vaule=()=>{
        console.log(this.input1.current.value)
      }
      render() {
        const { name, age, sex } = this.props /*接收传递过来的参数*/
        return (
          <div>
            <input ref={this.input1}></input>
            <button onClick={this.getInput1Vaule}>获取第一个input值</button>
            <input ref={this.input2}></input>
          </div>
        )
      }
    }
    ReactDOM.render(<GetInputValue />, document.getElementById('test'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Refs 是 React 提供的一种访问 DOM 元素或组件实例的方法。通过 Refs,我们可以在组件中直接访问到被 Refs 引用的 DOM 元素或组件实例,从而进行一些特殊的操作。 下面,我来举一个 React Refs 的案例: 假设我们有一个 Input 组件,需要在组件加载完毕后自动获取焦点,我们可以通过 Refs 来实现这个功能。具体的实现步骤如下: 1. 在 Input 组件中定义 Refs: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); // 创建 Refs } render() { return <input type="text" ref={this.inputRef} />; } } ``` 2. 在 componentDidMount 生命周期中,使用 Refs 获取到 input 元素,并调用 focus() 方法: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); // 创建 Refs } componentDidMount() { this.inputRef.current.focus(); // 获取 input 元素并获取焦点 } render() { return <input type="text" ref={this.inputRef} />; } } ``` 这样,当 Input 组件加载完毕后,它的 input 元素就会自动获取焦点了。 注意,Refs 只能在类组件中使用,不能在函数式组件中使用。另外,Refs 的值可以是回调函数,而不仅仅是 React.createRef() 方法返回的对象。如果使用回调函数的方式,可以在回调函数中访问到组件实例或 DOM 元素,例如: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = null; // 创建 Refs this.setInputRef = element => { this.inputRef = element; }; } componentDidMount() { this.inputRef.focus(); // 获取 input 元素并获取焦点 } render() { return <input type="text" ref={this.setInputRef} />; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值