react ref获取dom对象
step = React.createRef(); // init
<div ref={this.step}></div> // bind
componentDidMount() {
l(this.step.current.offsetHeight); // 获取数据
}
为 类 添加ref
这种方法是获取组件的实例,而不是组件的dom
class Hello extends Component {
log = () => {
l(1);
};
render() {
return (
<div>
<button>click me</button>
</div>
);
}
}
class Test extends Component {
btn = React.createRef();
componentDidMount() {
this.btn.current.log(); // 1
}
render() {
return (
<div>
<Hello ref={this.btn} />
</div>
);
}
}
函数组件使用 ref
function CustomTextInput(props) {
// 这里必须声明 textInput,这样 ref 回调才可以引用它
let textInput = null;
function handleClick() {
textInput.focus();
}
return (
<div>
<input
type="text"
ref={(input) => { textInput = input; }} />
<input
type="button"
value="Focus the text input"
onClick={handleClick}
/>
</div>
);
}
传递 refs
class Hello extends Component {
render() {
return (
<div>
<button ref={this.props.btnRef}>click me</button>
</div>
);
}
}
class Test extends Component {
btn = React.createRef();
componentDidMount() {
fromEvent(this.btn.current, "click").subscribe(v => l(v.type)); // click
}
render() {
return (
<div>
<Hello btnRef={this.btn} />
</div>
);
}
}
高阶组件中传递 ref
function withTest() {
return function(Target) {
class WithTest extends Component {
render() {
const { forwardedRef, ...rest } = this.props;
return (
<div>
233
<Target ref={forwardedRef} {...rest} />
</div>
);
}
}
function forwardRef(props, ref) {
return <WithTest {...props} forwardedRef={ref} />;
}
return React.forwardRef(forwardRef);
};
}
@withTest()
class Hello extends Component {
render() {
return (
<div>
<div>hello</div>
</div>
);
}
}
class Test extends Component {
btn = React.createRef();
componentDidMount() {
l(this.btn.current); // 获取到 Hello 的实例
}
render() {
return <Hello ref={this.btn} />;
}
}
react ref获取dom对象的更多相关文章
- React对比Vue(03 事件的对比,传递参数对比,事件对象,ref获取DOM节点,表单事件,键盘事件,约束非约束组件等)
import React from 'react'; class Baby extends React.Component { constructor (props) { super(props) t ...
- React 事件对象、键盘事件、表单事件、ref获取dom节点、react实现类似Vue双向数据绑定
1.案例实现代码 import React, { Component } from 'react'; /** * 事件对象.键盘事件.表单事件.ref获取dom节点.react实现类似Vue双向数据绑 ...
- 六、React 键盘事件 表单事件 事件对象以及React中的ref获取dom节点 、React实现类似Vue的双向数据绑定
接:https://www.cnblogs.com/chenxi188/p/11782349.html 事件对象 .键盘事件. 表单事件 .ref获取dom节点.React实现类似vue双向数据绑定 ...
- vue 使用ref获取DOM元素和组件引用
在vue中可以通过ref获取dom元素,并操作它,还可以获取组件里面的数据和方法. HTML部分: <div id="app"> <input type=&quo ...
- dom变成jquery对象 先获取dom对象 然后通过$()转换成jquery对象
dom变成jquery对象 先获取dom对象 然后通过$()转换成jquery对象
- js点滴知识(1) -- 获取DOM对象和编码
在今天的工作中发现了一些小的问题,在网上查了一下,才知道自己的js才是冰山一角,以后要虚心向他人学习,要虚怀若谷. 发现一:js获取DOM对象与jquery的区别 先前总以为,二者是一样的,最近才知道 ...
- ExtJs 获取Dom对象
对象指页面上的某一部分,如:Input等.我觉得在EXT JS中会有三类基本对象,htmlelement , EXT.Element和CompositeElement .分别解释一下: htmlele ...
- vue的数据双向绑定和ref获取dom节点
vue是一个MVVM的框架 业务逻辑代码即js部分是model部分, html是view部分. 当model改变的时候,view也会改变 view 改变是,model也会改变 <template ...
- Vue系列之 => ref获取DOM元素和组件
可以获取DOM元素,和组件中的数据,方法 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
随机推荐
- C# Draw multiple Lines
I would make a Line class having start and end point of the line in struct Point and make list of th ...
- Unity中InitializeOnLoad属性的妙用
InitializeOnLoad 属性应用的对象是 静态构造函数,它可以保证在编辑器启动的时候调用此函数.根据这个特性,可以在编辑器中设置定期的回调(帧更新),来实现类似watchFile的功能.这里 ...
- 04、常用RDD操作整理
常用Transformation 注:某些函数只有PairRDD只有,而普通的RDD则没有,比如gropuByKey.reduceByKey.sortByKey.join.cogroup等函数要根据K ...
- 通过自己定义MVC的Controller的Json转换器解决日期序列化格式问题
今日,在MVC框架下使用EasyUI的datagrid载入数据时,服务端返回的Json日期格式为 /Date(1433088000000+0800)/ .须要client进一步转换.并且也不符合Eas ...
- 安装了 R2 Integration Servic 之后,SQL Server 2008 Management Studio报错
问题产生 IM数据库服务器未安装Integration Servic,影响备份.在安装了安装了 SQL Server 2008 R2 Integration Servic 之后,SQL Server ...
- settings设置中文admin
所以更改setttings.py 下 LANGUAGE_CODE = 'zh-Hans'即可
- 在Centos7下安装nghttp2
如果是Ubuntu18.04, 系统本身已经带了nghttp2了, 直接apt安装就可以. 下载源代码 https://github.com/nghttp2/nghttp2 如果是在Ubuntu下编译 ...
- IOS开发中xib和StoryBoard的优缺点
总所周知,苹果官方为IOS开发提供了3种制作UI方式,让我们能够快速开发漂亮APP界面,每一种方式都有他们各自的特点,谁也不能代替谁.但是国内开发人员为此时争得不可开交. 大家各说各有理,说都想说服谁 ...
- 阿里云提示WordPress“/wp-includes/http.php输入IP验证不当”的解决办法
本文转自:https://www.liuzhishi.com/2931.html 标题: wordpress IP验证不当漏洞 简介: wordpress /wp-includes/http.php文 ...
- springboot nginx 配置
安装nginx参考本人另一篇博客:http://www.cnblogs.com/gmq-sh/p/5750833.html spring-boot需要启动nginx的,用于监听启动的端口.一.配置ng ...