欢迎来到代码驿站!

当前位置:首页 >

React中refs的一些常见用法汇总

时间:2022-02-23 09:42:13|栏目:|点击:

什么是Refs

Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法中创建的 React 元素。
Ref转发是一项将ref自动通过组件传递到子组件的技巧。 通常用来获取DOM节点或者React元素实例的工具。在React中Refs提供了一种方式,允许用户访问dom节点或者在render方法中创建的React元素。

Refs转发

Ref 转发是一个可选特性,其允许某些组件接收 ref,并将其向下传递(换句话说,“转发”它)给子组件。

默认情况下,不能在函数组件上使用 ref 属性,因为它们没有实例:

一、String 类型的 Refs

不建议使用,因为 string 类型的 refs 存在一些问题。它已过时并可能会在未来的版本被移除。

import React from "react";
// 父组件
export default class StringRef extends React.PureComponent {
  componentDidMount() {
    console.log("stringRefDom:", this.refs.stringRefDom);
    console.log("stringRefComp:", this.refs.stringRefComp);
  }
  render() {
    return (
      <div>
        {/*原生组件使用方式*/}
        <div ref={"stringRefDom"}>stringRefDom</div>
        {/*类组件使用方式*/}
        <StringRefComp ref={"stringRefComp"} />
      </div>
    );
  }
}
//类组件
class StringRefComp extends React.PureComponent {
  render() {
    return <div>StringRefComp</div>;
  }
}

二、回调 Refs

  • 如果 ref 回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次
  • 第一次传入参数 null,然后第二次会传入参数 DOM 元素
  • 这是因为在每次渲染时会创建一个新的函数实例,所以 React 清空旧的 ref 并且设置新的
  • 通过将 ref 的回调函数定义成 class 的绑定函数的方式可以避免上述问题
  • 但是大多数情况下它是无关紧要的
import React from "react";
// 父组件
export default class CallbackRef extends React.PureComponent {
  constructor(props) {
    super(props);
    this.callbackRefDom = null;
    this.callbackRefComp = null;
  }
  componentDidMount() {
    console.log("callbackRefDom:", this.callbackRefDom);
    console.log("callbackRefComp:", this.callbackRefComp);
  }
  //回调函数
  setCallbackRefDom = (ref) => {
    this.callbackRefDom = ref;
  };
  setCallbackRefComp = (ref) => {
    this.callbackRefComp = ref;
  };
  //回调函数
  render() {
    return (
      <div>
        <div ref={this.setCallbackRefDom}>callbackRefDom</div>
        <CallbackRefComp ref={this.setCallbackRefComp} />
      </div>
    );
  }
}

//类组件
class CallbackRefComp extends React.PureComponent {
  render() {
    return <div>callbackRefComp</div>;
  }
}

三、React.createRef()

  • React 16.3 版本引入
  • 较早版本的 React,推荐使用回调形式的 refs
import React from "react";
// 父组件
export default class CreateRef extends React.PureComponent {
  constructor(props) {
    super(props);
    this.createRefDom = React.createRef();
    this.createRefComp = React.createRef();
  }
  componentDidMount() {
    console.log("createRefDom:", this.createRefDom.current);
    console.log("createRefComp:", this.createRefComp.current);
  }
  render() {
    return (
      <div>
        <div ref={this.createRefDom}>createRefDom</div>
        <CreateRefComp ref={this.createRefComp} />
      </div>
    );
  }
}
//类组件
class CreateRefComp extends React.PureComponent {
  render() {
    return <div>CreateRefComp</div>;
  }
}

四、useRef

  • Hook 是 React 16.8 的新增特性
import React, { useEffect } from "react";
// 父组件
const UseRef = React.memo(() => {
  // // 同样可以用
  // const createRefDom = React.createRef();
  // const createRefComp = React.createRef();
  const createRefDom = React.useRef();
  const createRefComp = React.useRef();
  useEffect(() => {
    console.log("useRefDom:", createRefDom.current);
    console.log("useRefComp:", createRefComp.current);
  }, []);
  return (
    <div>
      <div ref={createRefDom}>useRefDom</div>
      <UseRefComp ref={createRefComp} />
    </div>
  );
});

export default UseRef;

//类组件
class UseRefComp extends React.PureComponent {
  render() {
    return <div>useRefComp</div>;
  }
}

五、Refs 与函数组件

  • 默认情况下,你不能在函数组件上使用 ref 属性,因为它们没有实例
  • 如果要在函数组件中使用 ref,你可以使用 forwardRef(可与 useImperativeHandle 结合使用)
  • 或者将该组件转化为 class 组件。
import React, { useEffect, useImperativeHandle } from "react";

// 父组件
const ForwardRef = React.memo(() => {
  const createRefComp = React.useRef();
  const createRefCompMethod = React.useRef();

  useEffect(() => {
    console.log("useRefComp:", createRefComp.current);
    console.log("createRefCompMethod:", createRefCompMethod.current);
    createRefComp.current.reload();
  }, []);
  return (
    <div>
      <ForwardRefFunc ref={createRefComp} />
    </div>
  );
});

export default ForwardRef;

const RefFunc = React.forwardRef((props, ref) => {
  const [name, setName] = React.useState(null);
  const reload = () => {
    console.log("reload");
    setTimeout(() => {
      setName("ForwardRefFunc");
    }, 3000);
  };
  //useImperativeHandle 可以让你在使用 ref 时自定义暴露给父组件的实例值
  useImperativeHandle(ref, () => {
    return {
      reload: reload,
    };
  });
  return <div ref={ref}>ForwardRefFunc {name}</div>;
});
const ForwardRefFunc = React.memo(RefFunc);

forwardRef 和 useImperativeHandle 最终目的是设法给 ref 提供一个可调用的对象!

总结

上一篇:2013年CIO需要知道的八句格言

栏    目:

下一篇:docker 容器上编译 go 程序提示找不到文件问题

本文标题:React中refs的一些常见用法汇总

本文地址:http://www.codeinn.net/misctech/194345.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有