
React组件核心属性之refs
发布日期:2021-05-07 01:01:56
浏览次数:26
分类:原创文章
本文共 4967 字,大约阅读时间需要 16 分钟。
组件内的标签可以通过ref属性来标识自己。
它有以下几种使用方式:
1、字符串形式的ref:
<input ref="input1"/>
完整代码:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <title>Document</title></head><body> <div id="app"></div> <script type="text/babel"> class Demo extends React.Component{ handleClick = () => { console.log(this); alert(this.refs.myInput.value); } render(){ return (<div> <input ref="myInput"/> <button onClick={this.handleClick}>点击获取</button> </div>) } } ReactDOM.render(<Demo/>, document.getElementById("app")) </script></body></html>
2、回调形式的ref:
<input ref={c => this.input1 = c} />
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <title>Document</title></head><body> <div id="app"></div> <script type="text/babel"> class Demo extends React.Component{ handleClick = () => { // console.log("this"); alert(this.myInput1.value); } render(){ return (<div> <input ref={c => this.myInput1 = c}/> <button onClick={this.handleClick}>点击获取</button> <input ref="myInput2"/> </div>) } } ReactDOM.render(<Demo/>, document.getElementById("app")) </script></body></html>
回调ref中调用次数的问题:
<script type="text/babel"> class Demo extends React.Component{ state = { isHot: false } changeWeather = () => { this.setState({ isHot: !this.state.isHot, ok: this.state.ok+1 }) } render(){ const {isHot} = this.state; return (<div> <input ref={c => {this.myInput1 = c; console.log(c)}}/> <h2>今天天气很<span>{isHot? "热" : "冷"}</span></h2> <button onClick={this.changeWeather}>点我切换天气</button> </div>) } } ReactDOM.render(<Demo/>, document.getElementById("app")) </script>
当你点击button时,控制台总是先打印出null,然后才打印出input。
但是,你把它定义成class绑定函数的形式可以避免上述问题:
class Demo extends React.Component{ state = { isHot: false } handleClick = c => { this.input1 = c; console.log(c); } changeWeather = () => { this.setState({ isHot: !this.state.isHot }) } render(){ const {isHot} = this.state; return (<div> <input ref={this.handleClick}/> <h2>今天天气很<span>{isHot? "热" : "冷"}</span></h2> <button onClick={this.changeWeather}>点我切换天气</button> </div>) } }
实际开发中,出现第一种情况问题不大,所以实际开发中,第一种方式用的较多。
3、createRef创建ref容器:
myRef = React.createRef()
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <title>Document</title></head><body> <div id="app"></div> <script type="text/babel"> class Demo extends React.Component{ myRef = React.createRef(); handleClick = () => { console.log(this); console.log(this.myRef.current.value); } render(){ return (<div> <input ref={this.myRef}/> <button onClick={this.handleClick}>点击获取</button> <input ref="myInput2"/> </div>) } } ReactDOM.render(<Demo/>, document.getElementById("app")) </script></body></html>
以上实例中,你在handleClick中将this打印出来,内容如下:
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年03月23日 03时10分32秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
oracle创建序列语法
2019-03-05
springboot通过控制层跳转页面404
2019-03-05
idea2020 没有 tomcat server
2019-03-05
jq动态修改元素的onclick属性的值
2019-03-05
为什么讨厌所谓仿生AI的说法
2019-03-05
ORACLE 客户端工具
2019-03-05
Elasticsearch下载慢?分享百度云下载-ELK
2019-03-05
云服务器springboot jar项目开启jmx remote监控-解决无法连接的问题
2019-03-05
文件上传-FileUpload
2019-03-05
快速排序
2019-03-05
Pyinstaller打包的exe文件过大的解决方法
2019-03-05
Linux的软链接跟Windows快捷方式一样?
2019-03-05
更改github的默认语言类型
2019-03-05
使用bigdecima实例化时传int和string时的精度丢失
2019-03-05
使用第三方sdk,微信wechat扫码登录
2019-03-05
mysql中的行转列
2019-03-05
java8中的闭包Function/BiFunction
2019-03-05
flink —— checkpoint机制
2019-03-05