博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react中的refs
阅读量:4548 次
发布时间:2019-06-08

本文共 4247 字,大约阅读时间需要 14 分钟。

概述

很久之前就知道refs,感觉好神秘,恰好今天突然发现字符串形式的ref在官网不推荐使用了,于是好好总结一下ref的用法,供以后开发时参考,相信对其他人也有用。

参考资料:

refs

在react数据流中,可以通过props,refs和Context来访问其它组件的属性,其中利用refs可以在数据流外强制修改组件实例。

需要注意的是,以前是通过给refs赋一个string,比如textInput,然后就可以通过this.refs.textInput来访问DOM节点,示例如下:

import React, { Component } from 'react';class NoControl extends Component {    constructor(props) {        super(props);        this.handleSubmit = this.handleSubmit.bind(this);    }    handleSubmit(e) {        e.preventDefault();        console.log(this.refs.name);    }    render() {        return(            
); }}export default NoControl;

但是官网不推荐使用这种形式使用refs了,并且这种方法在未来的版本可能会被移除。官方建议使用回调的形式代替这种使用方式。

refs的基本用法

一般说来,我们需要在constructor里面初始化一个ref,然后就能在return里面用了,示例如下:

class MyComponent extends React.Component {  constructor(props) {    super(props);    this.myRef = React.createRef();  }  render() {    return 
; }}

上面的this.myRef有一个current属性,它的值取决于下面三种情况:

  1. 如果ref属性被用于html元素,那么它的值是底层DOM元素。
  2. 如果ref属性被用于自定义类组件,那么它的值是已挂载的这个自定义类组件的实例。
  3. 函数式组件没有ref属性。

另外,通过ref我们还可以调用这个自定义类组件的方法,示例如下:

import React, { Component } from 'react';class CustomInput extends Component {    constructor(props) {        super(props);        this.handleFocus = this.handleFocus.bind(this);        this.myRef = React.createRef();    }    handleFocus(e) {        this.myRef.current.focus();    }    render() {        return(                    )    }}class NoControl extends Component {    constructor(props) {        super(props);        this.handleSubmit = this.handleSubmit.bind(this);        this.myRef = React.createRef();    }    handleSubmit(e) {        e.preventDefault();        this.myRef.current.handleFocus();    }    render() {        return(            
); }}export default NoControl;

refs的回调用法

由于refs里面的回调函数会在组件创建的时候自动生成。所以可以利用refs的回调用法,在组件创建的时候获得这个组件的一些信息,比如获得焦点等。示例如下:

import React, { Component } from 'react';class NoControl extends Component {    constructor(props) {        super(props);        this.handleSubmit = this.handleSubmit.bind(this);        this.setElement = this.setElement.bind(this);        this.myInput = null;    }    handleSubmit(e) {        e.preventDefault();        this.forceUpdate();    }    setElement(element) {        this.myInput = element;    }    componentDidMount() {        this.myInput.focus();    }        render() {        return(            
); }}export default NoControl;

refs转发

在使用HOC(高阶组件)的时候,我们是用一个类组件来对普通组件进行封装的,这个时候怎么获得里面这个普通组件的ref呢。

react官方提供了React.forwardRef这个api来实现。示例如下:

//NoControl.jsximport React, { Component } from 'react';import CustomInput from './CustomInput';class NoControl extends Component {    constructor(props) {        super(props);        this.handleSubmit = this.handleSubmit.bind(this);        this.myRef = React.createRef();    }    handleSubmit(e) {        e.preventDefault();        this.myRef.current.handleFocus();        this.forceUpdate();    }    render() {        return(            
); }}export default NoControl;//CustomInput.jsximport React, { Component } from 'react';function logProps(Component) { class LogProps extends React.Component { componentDidUpdate(prevProps) { console.log('old props:', prevProps); console.log('new props:', this.props); } render() { console.log('HOC exists;'); const { forwardedRef, ...rest } = this.props; return
; } } function forwardRef(props, ref) { return
; } return React.forwardRef(forwardRef);}class CustomInput extends Component { constructor(props) { super(props); this.handleFocus = this.handleFocus.bind(this); this.myRef = React.createRef(); } handleFocus(e) { this.myRef.current.focus(); } render() { return(
); }}export default logProps(CustomInput);

需要注意的是,forwardRef有第二个参数ref,然后它被React.forwardRef这个api封装,最后返回Component类组件。

转载于:https://www.cnblogs.com/yangzhou33/p/9633984.html

你可能感兴趣的文章
屌丝接盘侠们,孩子可能不是你们亲生的!
查看>>
BZOJ 1854 【SCOI2010】 游戏
查看>>
JavaScript - 匿名函数和闭包
查看>>
负载均衡下的资源文件配置/多站点下的资源文件夹共享(Windows IIS)
查看>>
MySQL firstmatch strategy
查看>>
MS SQL server 2014 创建用户及权限
查看>>
office很抱歉遇到一些临时服务器问题
查看>>
禁止键盘上的刷新键F5等
查看>>
SAP中对于获取订单的状态
查看>>
oracle PL/SQL块
查看>>
CentOS7集群环境Elastic配置
查看>>
【EXCEL】指定の項目の内容一覧を表示
查看>>
Head first java chapter 4 对象的行为
查看>>
luogu_4503【题解】企鹅QQ 哈希
查看>>
linux 面试
查看>>
Linux:Gentoo系统的安装笔记(三)
查看>>
打开IE窗口并最大化显示
查看>>
Windows API SendMessage 和 PostMessage 内部实现
查看>>
服务器发送邮件出现Could not connect to SMTP host错误 解决办法
查看>>
sklearn.preprocessing.LabelBinarizer
查看>>