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 ...
随机推荐
- .NET开源Protobuf-net组件修炼手册
一.前言 Protocol Buffer(简称Protobuf或PB) 是一个跨平台的消息交互协议,类似xml.json等 :别只会用Json和XML了,快来看看Google出品的Protocol B ...
- 如何修改maven的默认jdk版本
问题: 1.创建maven项目的时候,jdk版本是1.5版本,而自己安装的是1.7或者1.8版本. 2.每次右键项目名-maven->update project 时候,项目jdk版本变了,变回 ...
- 介绍三款串口监控工具:Device Monitoring Studio,portmon,Comspy
在开发上位机下位机通讯程序时,有一个好的监控工具会事半功倍.特在网上找了几款串口监控软件,作了简单对比: 一.Device Monitoring Studio 网址:http://www.hhdsof ...
- Java Web 清除缓存
res.setHeader("Cache-Control", "no-cache"); res.setHeader("Pragma", &q ...
- CMD批处理循环,太强大了(转)
终极dos批处理循环命令详解格式:FOR [参数] %%变量名 IN (相关文件或命令) DO 执行的命令 作用:对一个或一组文件,字符串或命令结果中的每一个对象执行特定命令,达到我们想要的结果. ...
- IndexDB 操作util
https://dexie.org/ https://www.tangshuang.net/5668.html https://github.com/tangshuang/hello-indexedd ...
- 第六周周赛——AK机会不易得,好好把握题解(出自HDU5650,codeforces 616A,624A,659A,655A,658A)
A题: A题题目链接 题目描写叙述: 位运算 TimeLimit:1000MS MemoryLimit:65536KB 64-bit integer IO format:%I64d Problem ...
- maven项目,httpclient jar包冲突
包含httpclient的jar包 org.apache.thrift:libthrift org.jboss.resteasy:resteasy-jaxrs com.alibaba:dubbo ma ...
- apache配置伪静态Rewrite
1: 修改apache的httpd.conf文件 找到这一行 #LoadModule rewrite_module modules/mod_rewrite.so 改成 LoadModule rewri ...
- WPF中,输入完密码回车提交 ,回车触发按钮点击事件
类似与winform中窗体的AcceptButton属性,在wpf中,需要将按钮的IsDefault设置为true就行.