美文网首页
React 如何使用ref

React 如何使用ref

作者: coderhzc | 来源:发表于2021-10-25 13:01 被阅读0次

1.ref = "字符串"获取DOM -- 不推荐使用

import React, { PureComponent } from 'react';


// 第一种获取refs (DOM) 
class App extends PureComponent {
  render() {
    return (
      <div>
        {/* <h1 ref=字符串/对象/函数>Hello,React</h1> */}
        {/* 1. 等于一个字符串 */}
        <h1 ref="titleRef">Hello,React</h1>
        <button onClick={e => this.changeText()}>字符串改变文本</button>
      </div>
    );
  }
  changeText() {
    console.log(this.refs.titleRef.innerHTML) // Hello,React
    this.refs.titleRef.innerHTML = "Hello,Huzhenchu"

  }
}
export default App;

实际截图:

image.png

2. 对象的方式,React推荐的做法

import React, { PureComponent } from 'react';
// 1.第二种获取refs (DOM)
import { createRef } from 'react';


class App extends PureComponent {
  constructor(props) {
    super(props)
    // 2.
    this.titleRef = createRef()
  }
  render() {
    return (
      <div>
        {/* <h1 ref=字符串/对象/函数>Hello,React</h1> */}
        {/* 3. 等于一个对象 目前React推荐的 */}
        <h1 ref={this.titleRef}>Hello,React</h1>
        <button onClick={e => this.changeText()}>对象改变文本</button>
      </div>
    );
  }
  changeText() {
    console.log(this.titleRef)
    console.log(this.titleRef.current)
    this.titleRef.current.innerHTML = "hello,huzhenchu"

  }
}
export default App;

实际截图:

image.png

3. refs 函数的使用

import React, { PureComponent } from 'react';
// 1.第三种获取refs 函数获取 (DOM)

class App extends PureComponent {
    constructor(props) {
        super(props)
        // 定义一个初始值
        this.titleEl = null
    }
    render() {
        return (
            <div>
                {/* <h1 ref=字符串/对象/函数>Hello,React</h1> */}
                {/* 3. 等于一个函数的使用 */}
                <h1 ref={arg => this.titleEl = arg}>Hello,React</h1>
                <button onClick={e => this.changeText()}>函数改变文本</button>
            </div>
        );
    }
    changeText() {
        console.log(this.titleEl)
        this.titleEl.innerHTML = "Hello,zhuzhenchu"
    }
}
export default App;

实际截图:

image.png

4. refs组件中的使用class组件的使用,函数组件是不支持的,因为函数组件是没有实例的

import React, { createRef, PureComponent } from 'react';

class Counter extends PureComponent {
  constructor(props) {
    super(props)
    this.state = {
      count: 0
    }
  }
  render() {
    const { count } = this.state
    return (
      <div>
        <h2> {count} </h2>
        <button onClick={e => this.childAddClick()}>子组件的点击事件</button>
      </div>
    )
  }
  childAddClick() {
    this.setState({
      count: this.state.count + 1
    })
  }
}

class App extends PureComponent {
  constructor(props) {
    super(props)
    this.titleRef = createRef()
  }
  render() {
    return (
      <div>
        {/* 1.对象的实现 */}
        <Counter ref={this.titleRef} />
        <hr />
        <button onClick={e => this.fatherAddClick()}>父组件的点击事件</button>

      </div>
    );
  }
  fatherAddClick() {
    console.log(this.titleRef)
    this.titleRef.current.childAddClick()
  }
}
export default App;

实际截图:

image.png

5. ref的转发-组件内容的补充

import React, { PureComponent, createRef } from 'react'

class Home extends PureComponent {
  render() {
    return (
      <div>
        <h1>Hello World</h1>
      </div>
    )
  }
}

// 函数组件  (函数组件用ref 是获取不到当前实例的)
function Profile(props) {
  return <h2>Profile</h2>
}

export default class App extends PureComponent {
  constructor(props) {
    super(props)
    this.titleRef = createRef()
    this.homeRef = createRef()
    this.profileRef = createRef()
  }
  render() {
    return (
      <div>
        <h1 ref={this.titleRef}>Hello World</h1>
        <Home ref={this.homeRef} />
        <Profile ref={this.profileRef}/>
        <button onClick={e => this.printRef()}>打印Ref</button>
      </div>
    )
  }
  printRef() {
    console.log(this.titleRef.current);
    console.log(this.homeRef.current);
    /**
     * index.js:1 Warning: Function components cannot be given refs. Attempts to access this ref will                   
       fail. Did you mean to use React.forwardRef()?
     * 警告:函数组件不能给出引用。 尝试访问此ref将失败。 您是想使用React.forwardRef()吗? 
     * 他是一个函数组件,没有实例的所以拿不到这个东西的
     * **/
    console.log(this.profileRef.current);
  }
}

实际截图

image.png

二.函数式组件是不支持 ref 这种写法的

import React, { PureComponent, createRef } from 'react'

class Home extends PureComponent {
  render() {
    return (
      <div>
        <h1>Hello World</h1>
      </div>
    )
  }
}

// 函数组件  (函数组件用ref 是获取不到当前实例的)
function Profile(props) {
  return <h2>Profile</h2>
}

export default class App extends PureComponent {
  constructor(props) {
    super(props)
    this.titleRef = createRef()
    this.homeRef = createRef()
    this.profileRef = createRef()
  }
  render() {
    return (
      <div>
        <h1 ref={this.titleRef}>Hello World</h1>
        <Home ref={this.homeRef} />
        <Profile ref={this.profileRef}/>
        <button onClick={e => this.printRef()}>打印Ref</button>
      </div>
    )
  }
  printRef() {
    console.log(this.titleRef.current);
    console.log(this.homeRef.current);
    console.log(this.profileRef.current);   
  }
}

实际截图

image.png

三 如果你想获取到ref的话 需要导入 React的一个库 forwardRef函数库

// 1.引入forwardRef函数库
import React, { PureComponent, createRef,forwardRef} from 'react'

class Home extends PureComponent {
  render() {
    return (
      <div>
        <h1>Hello World</h1>
      </div>
    )
  }
}

// 函数组件  (函数组件用ref 是获取不到当前实例的)
// 2.forwardRef 函数获取
const Profile = forwardRef(function Profile(props,ref) {
  return <h2 ref={ref}>Profile</h2>
})


export default class App extends PureComponent {
  constructor(props) {
    super(props)
    this.titleRef = createRef()
    this.homeRef = createRef()
    // 3.
    this.profileRef = createRef()
  }
  render() {
    return (
      <div>
        <h1 ref={this.titleRef}>Hello World</h1>
        <Home ref={this.homeRef} />
        {/* 4. 绑定ref */}
        <Profile ref={this.profileRef}/>
        <button onClick={e => this.printRef()}>打印Ref</button>
      </div>
    )
  }
  printRef() {
    console.log(this.titleRef.current);
    console.log(this.homeRef.current);
    // 5.打印
    console.log(this.profileRef.current);   
  }
}

实际截图

image.png

相关文章

  • React 如何使用ref

    1.ref = "字符串"获取DOM -- 不推荐使用 实际截图: 2. 对象的方式,React推荐的做法 实际截...

  • React学习笔记(三)

    React中的 ref 的使用 ref是一个引用,在React中使用ref来直接获取DOM元素从而操作DOM Re...

  • 如何使用React Refs

    如何在React中利用ref以及ref转发,回调ref和HOC React Ref是有用的功能,可作为从父组件中引...

  • React 中 ref 的使用

    ref是reference的简写,它是一个引用,在react中,我们使用ref来操作DOM。 在react中,我们...

  • React Ref使用

    如何通过refs获得对应的DOM? 使用时通过 this.refs.传入的字符串格式获取对应的元素,使用时 thi...

  • react自定义组件中使用ref

    一、自定义组件使用ref并且透传子组件ref 自定义组件中使用ref需要用到react的2个hooks:1.for...

  • 组件ref的使用

    自己封装的组件,想使用ref,调用内部的方法1.使用React.forwardRef,包裹组件。 2.接收ref属...

  • react-ref

    ref:react对真实dom的引用 ref在reactElement上 ref在react组件上

  • ReactJs中获取dom元素

    使用传统的React语法 this.mydom = React.createRef() 创建一个变量 ref={k...

  • React中ref的使用

    React中Ref是什么? ref是React提供的用来操纵React组件实例或者DOM元素的接口。 ref的作用...

网友评论

      本文标题:React 如何使用ref

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