React之ref的使用
来源:互联网 发布:贵金属喊单软件 编辑:程序博客网 时间:2024/06/06 19:39
ES6回调函数
这里我们使用ES6回调函数实现获取焦点
var MyComponent = React.createClass({
handleClick: function() {
// Explicitly focus the text input using the raw DOM API.
if (this.myTextInput !== null) {
this.myTextInput.focus();
}
},
render: function() {
return (
<div>
<input type="text" ref={ (ref)=>this.myTextInput = ref } />
<input
type="button"
value="Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
CommonJs回调函数实现
var MyComponent = React.createClass({
handleClick: function() {
// Explicitly focus the text input using the raw DOM API.
if (this.myTextInput !== null) {
this.myTextInput.focus();
}
},
render: function() {
return (
<div>
<input type="text" ref={ function(ref){this.myTextInput = ref}.bind(this) } />
<input
type="button"
value="Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
注意:在上面代码中,使用的是CommonJs语法,回调函数function(){}后面有.bind(this)。这是需要注意的地方,绑定this,使function内的this对象是该组件。如果不绑定this,那么在handleClick中的this.myTextInput将会报未定义的错误。这是需要注意的地方,在ES6中就不存在这个问题。
本文的目的就是通过实例来介绍ref回调函数如何使用,希望本文对大家有所帮助。
- React之ref的使用
- react-native 之 ref 的使用
- react入坑之ref的使用
- react native ref的使用
- react中的ref的使用
- React中Ref的使用
- React 中 ref 操作DOM的使用
- React 之ref 详解
- React之ref详细用法
- React之ref详细用法
- React学习之进阶ref的必要性(十三)
- React学习之进阶ref的必要性(十三)
- ReactJS学习系列课程(React ref的使用)
- react native key,ref,bind的作用和使用
- ReactJS学习系列课程(React ref的使用)
- Vue之ref与refs的使用
- React Native ref高级用法&&setNativeProps使用
- React Native ref高级用法&&setNativeProps使用
- 《Python基础教程》读书笔记(4)之第9章魔法方法、属性和迭代器(关键词:Python/魔法方法/属性/迭代器)
- 防止SQL注入和XSS攻击Filter
- jaris flv player和ckplayer
- 如何在Angular4中引入jquery
- 最大子序列和的四种不同复杂度实现
- React之ref的使用
- 初探Android中的请求服务器并解析返回的json数据
- Elasticsearch之确切值(Exact values)、全文文本(full text)、倒排索引(inverted index)。
- 命名规则
- 从零开始微信机器人(二):使用图灵机器人和api.ai相关接口
- js在html标签中参数无效解决方案
- 【C/C++开发】模板类
- JAVA synchronized实现原理以及其中锁优化的归纳总结
- linux配置.bashrc美化终端命令行(shell)提示符