React中Ref 的使用方法详解


Posted in Javascript onApril 28, 2020

本文实例讲述了React中Ref 的使用方法。分享给大家供大家参考,具体如下:

React中Ref 的使用 React v16.6.3

在典型的React数据流中,props是父组件与其子组件交互的唯一方式。要修改子项,请使用new props 重新呈现它。但是,在某些情况下,需要在典型数据流之外强制修改子项。要修改的子项可以是React组件的实例,也可以是DOM元素。对于这两种情况,React都提供了api。

何时使用refs

refs有一些很好的用例:

  • 1.文本选择或媒体播放。
  • 2.触发势在必行的动画。
  • 3.与第三方DOM库集成。

避免将refs用于可以声明性地完成的任何操作。

*不要过度使用Refs

旧版API:字符串引用

如果您之前使用过React,那么您可能熟悉一个旧的API,其中ref属性是一个字符串"textInput",并且DOM节点被访问为this.refs.textInput。建议不要使用它,因为字符串引用有一些问题,被认为是遗留问题,很可能会在未来的某个版本中删除。

回调引用

当组件安装时,React将使用DOM元素调用ref回调,并在卸载时调用null。

在componentDidMount或componentDidUpdate触发之前,Refs保证是最新的.

class CustomTextInput extends React.Component {
 constructor(props) {
 super(props);

 this.textInput = null;

 this.setTextInputRef = element => {
  this.textInput = element;
 };

 this.focusTextInput = () => {
  // Focus the text input using the raw DOM API
  if (this.textInput) this.textInput.focus();
 };
 }

 componentDidMount() {
 // autofocus the input on mount
 this.focusTextInput();
 }

 render() {
 // Use the `ref` callback to store a reference to the text input DOM
 // element in an instance field (for example, this.textInput).
 return (
  <div>
  <input
   type="text"
   ref={this.setTextInputRef}
  />
  <input
   type="button"
   value="Focus the text input"
   onClick={this.focusTextInput}
  />
  </div>
 );
 }
}

refs例子--点击获取input焦点

class Example extends React.Component {
 handleClick() {
 // 使用原生的 DOM API 获取焦点
 this.refs.myInput.focus
 ();
 }
 render() {
 // 当组件插入到 DOM 后,ref 属性添加一个组件的引用于到 this.refs
 return (
  <div>
  <input type="text" ref="myInput" />
  <input
   type="button"
   value="点我输入框获取焦点"
   onClick={this.handleClick.bind(this)}
  />
  </div>
 );
 }
}

使用React.createRef()

React.createRef()React 16.3中引入的API。如果您使用的是早期版本的React,我们建议您使用回调引用。

创建React.createRef()

Refs是使用属性创建的,React.createRef()并通过ref属性附加到React元素。在构造组件时,通常将Refs分配给实例属性,以便可以在整个组件中引用它们。

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

访问ref

当ref被传递给元素时render,对该节点的引用变得可以在currentref 的属性处访问

const node = this.myRef.current;

ref的值根据节点的类型而有所不同

  • 当在refHTML元素上使用该属性时,ref在构造函数中创建的属性将React.createRef()接收底层DOM元素作为其current属性。
  • 在ref自定义类组件上使用该属性时,该ref对象将接收组件的已安装实例作为其current。

您可能无法ref在函数组件上使用该属性,因为它们没有实例。

class CustomTextInput extends React.Component {
 constructor(props) {
 super(props);
 // create a ref to store the textInput DOM element
 this.textInput = React.createRef();
 this.focusTextInput = this.focusTextInput.bind(this);
 }

 focusTextInput() {
 // Explicitly focus the text input using the raw DOM API
 // Note: we're accessing "current" to get the DOM node
 this.textInput.current.focus();
 }

 render() {
 // tell React that we want to associate the <input> ref
 // with the `textInput` that we created in the constructor
 return (
  <div>
  <input
   type="text"
   ref={this.textInput} />

  <input
   type="button"
   value="Focus the text input"
   onClick={this.focusTextInput}
  />
  </div>
 );
 }
}

current当组件安装时,React将为该属性分配DOM元素,并null在卸载时将其分配回。ref更新发生之前componentDidMount或componentDidUpdate生命周期方法。

无法在函数组件上使用ref属性

function MyFunctionComponent() {
 return <input />;
}

class Parent extends React.Component {
 constructor(props) {
 super(props);
 this.textInput = React.createRef();
 }
 render() {
 // This will *not* work!
 return (
  <MyFunctionComponent ref={this.textInput} />
 );
 }
}

**如果需要引用它,则应该将组件转换为类,就像您需要生命周期方法或状态时一样。
但是,只要引用DOM元素或类组件,就可以在函数组件中使用该ref属性:**

function CustomTextInput(props) {
 // textInput must be declared here so the ref can refer to it
 let textInput = React.createRef();

 function handleClick() {
 textInput.current.focus();
 }

 return (
 <div>
  <input
  type="text"
  ref={textInput} />

  <input
  type="button"
  value="Focus the text input"
  onClick={handleClick}
  />
 </div>
 );
}

将DOM引用公开给父组件

在极少数情况下,可能希望从父组件访问子节点的DOM节点。通常不建议这样做,因为它会破坏组件封装,但它偶尔可用于触发焦点或测量子DOM节点的大小或位置。

虽然可以向子组件添加引用,但这不是一个理想的解决方案,因为只能获得组件实例而不是DOM节点。此外,这不适用于功能组件。

如果使用React 16.3或更高版本,我们建议在这些情况下使用ref forwarding。引用转发允许组件选择将任何子组件的引用公开为自己的组件。可以在ref转发文档中找到有关如何将子DOM节点公开给父组件的详细示例。

如果您使用React 16.2或更低版本,或者您需要比ref转发提供的更多灵活性,您可以使用此替代方法并明确地将ref作为不同名称的prop传递。

如果可能,建议不要暴露DOM节点,但它可以是一个有用的逃生舱。请注意,此方法要求向子组件添加一些代码。如果您完全无法控制子组件实现,则最后一个选项是使用findDOMNode(),但不鼓励使用它StrictMode。

希望本文所述对大家react程序设计有所帮助。

Javascript 相关文章推荐
jquery tools之tabs 选项卡/页签
Jul 25 Javascript
JQuery Tips相关(1)----关于$.Ready()
Aug 14 Javascript
jQuery Mobile 触摸事件实例
Jun 04 Javascript
jQuery简单实现点击文本框复制内容到剪贴板上的方法
Aug 01 Javascript
js删除Array数组中指定元素的两种方法
Aug 03 Javascript
jQuery实现的超链接提示效果示例【附demo源码下载】
Sep 09 Javascript
jQuery弹出层插件popShow用法示例
Jan 23 Javascript
用 js 的 selection range 操作选择区域内容和图片
Apr 18 Javascript
红黑树的插入详解及Javascript实现方法示例
Mar 26 Javascript
vue移动端下拉刷新和上拉加载的实现代码
Sep 08 Javascript
详解element上传组件before-remove钩子问题解决
Apr 08 Javascript
vue项目中的支付功能实现(微信支付和支付宝支付)
Feb 18 Vue.js
在Webpack中用url-loader处理图片和字体的问题
Apr 28 #Javascript
react PropTypes校验传递的值操作示例
Apr 28 #Javascript
vue 百度地图(vue-baidu-map)绘制方向箭头折线实例代码详解
Apr 28 #Javascript
Vue + Scss 动态切换主题颜色实现换肤的示例代码
Apr 27 #Javascript
浅析vue cli3 封装Svgicon组件正确姿势(推荐)
Apr 27 #Javascript
react组件基本用法示例小结
Apr 27 #Javascript
react基本安装与测试示例
Apr 27 #Javascript
You might like
2019年漫画销量排行榜:鬼灭登顶 海贼单卷制霸 尾田盛赞鬼灭
2020/03/08 日漫
六酷社区论坛HOME页清新格调免费版 下载
2007/03/07 PHP
php adodb连接mssql解决乱码问题
2009/06/12 PHP
php setcookie(name, value, expires, path, domain, secure) 参数详解
2013/06/28 PHP
php魔术方法功能与用法实例分析
2016/10/19 PHP
PHP+MySQL实现输入页码跳转到指定页面功能示例
2018/06/01 PHP
js实现的标题栏新消息闪烁提示效果
2014/06/06 Javascript
中文输入法不触发onkeyup事件的解决办法
2014/07/09 Javascript
Bootstrap按钮组件详解
2016/04/26 Javascript
AngularJS 自定义过滤器详解及实例代码
2016/09/14 Javascript
Bootstrap模态框(Modal)实现过渡效果
2017/03/17 Javascript
使用 Javascript 实现浏览器推送提醒功能的示例
2017/11/03 Javascript
Element-ui table中过滤条件变更表格内容的方法
2018/03/02 Javascript
nodejs 日志模块winston的使用方法
2018/05/02 NodeJs
[07:47]DOTA2国际邀请赛采访专栏:探访Valve总部
2013/08/08 DOTA
详解Python编程中对Monkey Patch猴子补丁开发方式的运用
2016/05/27 Python
python中装饰器级连的使用方法示例
2017/09/29 Python
python操作列表的函数使用代码详解
2017/12/28 Python
python自动截取需要区域,进行图像识别的方法
2018/05/17 Python
三步实现Django Paginator分页的方法
2019/06/11 Python
python 使用plt画图,去除图片四周的白边方法
2019/07/09 Python
django之自定义软删除Model的方法
2019/08/14 Python
python 命令行传入参数实现解析
2019/08/30 Python
详解CSS3 Media Queries中媒体属性的使用
2016/02/29 HTML / CSS
Shopee新加坡:东南亚与台湾电商平台
2019/01/25 全球购物
新加坡网上美容店:Hermo新加坡
2019/06/19 全球购物
斯洛伐克最大的婴儿食品和用品网上商店:Feedo.sk
2020/12/21 全球购物
Can a struct inherit from another class? (结构体能继承类吗)
2014/07/22 面试题
总经理驾驶员岗位职责
2013/12/04 职场文书
电大毕业个人生自我鉴定
2014/03/26 职场文书
幼儿园运动会口号
2014/06/07 职场文书
党支部活动策划方案
2014/08/18 职场文书
水电施工员岗位职责
2015/04/11 职场文书
校运会新闻稿
2015/07/17 职场文书
八年级物理教学反思
2016/02/19 职场文书
SpringBoot 拦截器妙用你真的了解吗
2021/07/01 Java/Android