概述

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

参考资料:

Refs & DOM

Forwarding Refs

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(
<form onSubmit={this.handleSubmit}>
<input type="text" ref="name"/>
<button type="submit">Submit</button>
</form>
);
}
} export default NoControl;

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

refs的基本用法

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

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

上面的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(
<input type="text" ref={this.myRef}/>
)
} } 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(
<form onSubmit={this.handleSubmit}>
<CustomInput ref={this.myRef}/>
<button type="submit">Submit</button>
</form>
);
}
} 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(
<form onSubmit={this.handleSubmit}>
<input type="text" ref={this.setElement}/>
<button type="submit">Submit</button>
</form>
);
}
} export default NoControl;

refs转发

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

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

//NoControl.jsx
import 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(
<form onSubmit={this.handleSubmit}>
<CustomInput ref={this.myRef}/>
<button type="submit">Submit</button>
</form>
);
}
} export default NoControl; //CustomInput.jsx
import 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 <Component ref={forwardedRef} {...rest} />;
}
}
function forwardRef(props, ref) {
return <LogProps {...props} forwardedRef={ref} />;
}
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(
<input type="text" ref={this.myRef}/>
);
}
} export default logProps(CustomInput);

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

react中的refs的更多相关文章

  1. React 中的 refs的应用

    React Refs React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上. 这个特殊的属性允许你引用 render() 返回的相应的支撑实例( back ...

  2. 【React】282- 在 React 组件中使用 Refs 指南

    英文:Yomi Eluwande  译文:joking_zhang https://segmentfault.com/a/1190000019277029 使用 React 时,我们的默认思维方式应该 ...

  3. 在 React 组件中使用 Refs 指南

    原文:Fullstack React's Guide to using Refs in React Components作者:Yomi Eluwande译者:博轩 译文:https://segment ...

  4. react中input自动聚焦问题

    input自动聚焦问题 在react中可以使用refs解决这个问题,首先看一下refs的使用场景: (1)处理焦点.文本选择或媒体控制. (2)触发强制动画. (3)集成第三方 DOM 库. 使用re ...

  5. 九、React中的组件、父子组件、React props父组件给子组件传值、子组件给父组件传值、父组件中通过refs获取子组件属性和方法

    一.概述 React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. [父子组件]:组件的相互调用中,我们把调用者称为父 ...

  6. react中这些细节你注意过没有?

    react中的一些细节知识点: 1.组件中get的使用(作为类的getter) ES6知识:class类也有自己的getter和setter,写法如下: Class Component { const ...

  7. React中ref的使用方法

    React中ref的使用方法 在react典型的数据流中,props传递是父子组件交互的唯一方式:通过传递一个新的props值来使子组件重新re-render,从而达到父子组件通信.当然,就像reac ...

  8. React中setState学习总结

    react中setState方法到底是异步还是同步,其实这个是分在什么条件下是异步或者同步. 1.先来回顾一下react组件中改变state的几种方式: import React, { Compone ...

  9. react中父组件给子组件传值

    子组件 state = { msg: 'a' } render(){ return<h1>{this.state.msg}</h1> } setInfo = (val)=> ...

随机推荐

  1. 7C - 折线分割平面

    我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面分成7部分,具体如下所示.  Input ...

  2. Requset模块

    Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库 各种请求方式: #!/urs/bin/evn python # -*- cod ...

  3. python基础 (初识函数&函数进阶)

    函数基础部分 .什么是函数? 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率. 2.定义函数 定义:def 关键词开头,空格之后接函数名 ...

  4. UVa540

    //先输入队伍的个数 //用map建立数组将队伍序号和个人序号相互对应 //三条命令 #include <bits/stdc++.h> using namespace std; ; int ...

  5. python中的列表及numpy数组排序

    一.列表排序  # python中对列表排序有sort.sorted两种方法,其中sort是列表内置方法,其帮助文档如下:In [1]: help(sorted) Help on built-in f ...

  6. 浅析b-树 b+树 以及Mysql的Innodb,Myisam引擎

    B-树性质 B-树可以看作是对2-3查找树的一种扩展,即他允许每个节点有M-1个子节点. 1根节点至少有两个子节点 2每个节点有M-1个key,并且以升序排列 3位于M-1和M key的子节点的值位于 ...

  7. 分布式服务治理框架dubbo

    Dubbo最主要功能有两个 1 RPC调用 2 SOA服务治理方案 Dubbo的架构 Dubbo常见的注册中心有2中,zookeeper以及redis 这篇文章讲解的是采用的zookeeper,要求读 ...

  8. 【机器学习】随机森林 Random Forest 得到模型后,评估参数重要性

    在得出random forest 模型后,评估参数重要性 importance() 示例如下 特征重要性评价标准 %IncMSE 是 increase in MSE.就是对每一个变量 比如 X1 随机 ...

  9. Scrum冲刺阶段6

    成员今日完成的任务 人员 任务 何承华 学习后端设计 陈宇 后端设计 丁培辉 学习后端设计 温志铭 信息界面设计 杨宇潇 信息界面界面设计 张主强 服务器构建学习 成员遇到的问题 人员 问题 何承华 ...

  10. [转]etcd 启用 https

    1, 生成 TLS 秘钥对 2,拷贝密钥对到所有节点 3,配置 etcd 使用证书 4,测试 etcd 是否正常 5,配置 kube-apiserver 使用 CA 连接 etcd 6,测试 kube ...