美文网首页
refs是什么,如何使用,需要注意什么

refs是什么,如何使用,需要注意什么

作者: 忆往夕 | 来源:发表于2021-01-08 14:39 被阅读0次

Refs是什么?

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

何时适合使用 Refs?

  • 管理焦点,文本选择或媒体播放。
  • 触发强制动画
  • 集成第三方 DOM 库。

Refs有哪些使用方式?

  • 原生DOM元素上使用Ref
  • 类组件上使用Ref
  • 函数组件上使用Ref
  • 高阶组件上使用Ref
  • 函数组件使用HOOK useRef
import React, { Component, useRef } from "react";

export default class RefPage extends Component {
  constructor(props) {
    super(props);

    this.state = {
      count: 0,
    };
    this.nameInputRef = React.createRef();
    this.passwordRef = React.createRef();
    this.ageInputRef = React.createRef();
    this.countryRef = React.createRef();
  }

  submit = (e) => {
    const name = this.nameInputRef.current.value;
    const password = this.passwordRef.current.getPassword();
    const age = this.ageInputRef.current.value;
    let country = this.countryRef.current.value;

    console.log("Submit", name, password, age, country); //, password, age, country, galaxy);
  };

  render() {
    const AgeInputRef = React.forwardRef(AgeInput);
    const HocCountry = hoc(CountryInput);

    return (
      <div>
        <div>
          <label htmlFor="">姓名</label>
          <input type="text" ref={this.nameInputRef} />
        </div>
        <PasswordInput ref={this.passwordRef} />
        <AgeInputRef ref={this.ageInputRef} />
        <BirthInput />
        <CityInput />
        <HocCountry label="国家" ref={this.countryRef} />
        <div>
          <button onClick={this.submit}>提交</button>
        </div>
      </div>
    );
  }
}

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

  getPassword = () => {
    return this.passwordInputRef.current.value;
  };

  render() {
    return (
      <div>
        <div>
          <label htmlFor="">密码</label>
          <input type="text" ref={this.passwordInputRef} />
        </div>
      </div>
    );
  }
}

function AgeInput(props, ref) {
  return (
    <div>
      <label htmlFor="">年龄</label>
      <input type="text" ref={ref} />
    </div>
  );
}

class BirthInput extends Component {
  constructor(props) {
    super(props);
    this.birthInput = null;
  }

  setTextInputRef = (ele) => {
    console.log("setTextInputRef=======", ele);
    this.birthInput = ele;
  };

  componentDidMount() {
    this.birthInput.value = "123";
    this.birthInput.focus();
  }

  render() {
    return (
      <div>
        <div>
          <label htmlFor="">生日</label>
          <input type="text" ref={this.setTextInputRef} />
          {/* 不建议使用内联 */}
          <input
            type="text"
            ref={(ele) => {
              console.log("setTextInputRef", ele);
              this.birthInput = ele;
            }}
          />
          <button
            onClick={() => {
              let val = this.birthInput.value;
              console.log("birth", val);
            }}
          >
            Click
          </button>
        </div>
      </div>
    );
  }
}

//hook用法
function CityInput(props) {
  const ref = useRef(null);

  return (
    <div>
      <label htmlFor="">区域</label>
      <input type="text" ref={ref} />
      <button
        onClick={() => {
          const value = ref.current.value;
          console.log("city", value); //sy-log
        }}
      >
        click
      </button>
    </div>
  );
}

const hoc = (WrapComponent) =>
  React.forwardRef((props, ref) => {
    return (
      <div>
        <WrapComponent {...props} countryInputRef={ref} />
      </div>
    );
  });

function CountryInput({ label, countryInputRef }) {
  return (
    <div>
      <label htmlFor="">{label}</label>
      <input type="text" ref={countryInputRef} />
    </div>
  );
}

本文是基于React17.0版本

相关文章

  • refs是什么,如何使用,需要注意什么

    Refs是什么? Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法中创建的 React...

  • vue 容易忽略的点

    关于this.$refs获取dom节点在mounted中使用this.$refs需要注意,虽然mounted表示d...

  • $refs是什么?

    作用:可以获取到具体某一个元素(在javascript里直接访问一个子组件) 用法:在html元素上赋予ref这个...

  • React Ref使用

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

  • 最近工作和思考的重点

    全链路是什么,如何做,需要注意什么,如何落地。如何利用机器学习AI来提升反洗钱的能力,找到合适的切入点和适当的使用...

  • iOS底层第八、九、十天 -- Block

    引导语: Block原理是怎样的?本质是什么? _block的作用是什么?使用需要注意什么? block的属性修饰...

  • 如何使用React Refs

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

  • React中Refs的使用方法

    欢迎访问主页,有更多文章内容转载请注明原出处原文链接地址:React中Refs的使用方法 什么是Refs Refs...

  • 父组件访问子组件的方式$refs

    一、$refs的使用 $refs和ref是一起使用的,通过ref给子组件绑定一个id,使用this.$refs.i...

  • 一个使用$refs需要注意的问题

    一、问题的提出 在vue文件中给一个组件设置了ref属性,目的是后续代码中能够触发该组件上的一些自带方法: ...

网友评论

      本文标题:refs是什么,如何使用,需要注意什么

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