refs的简单使用
1.字符串形式的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id= 'test'></div>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<!-- 生产环境中不建议使用 -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<!-- 引入prop-types,用于对标签属性进行限制 -->
<!-- <script type="text/javascript" src="..js/prop-types.js"></script> -->
<script src="https://cdn.bootcdn.net/ajax/libs/prop-types/15.8.1/prop-types.js"></script>
<script type="text/babel">
// 创建
class Demo extends React.Component {
// 展示左侧输入框的数据
showData = ()=>{
console.log(this.refs)
const {input1}=this.refs
alert(input1.value)
}
// 展示右侧输入框的数据
showData2=()=>{
const {input2}=this.refs
alert(input2.value)
}
render(){
return (
<div >
<input ref="input1" type = "text" placeholder="点击按钮提示数据"/>
<button ref="button100" type="button" onClick={this.showData}>点击按钮提示左侧数据</button>
<input ref="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/>
</div>
)
}
}
// 渲染
ReactDOM.render(<Demo/>,document.getElementById('test'))
</script>
</body>
</html>
2.回调函数形式的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id= 'test'></div>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<!-- 生产环境中不建议使用 -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<!-- 引入prop-types,用于对标签属性进行限制 -->
<!-- <script type="text/javascript" src="..js/prop-types.js"></script> -->
<script src="https://cdn.bootcdn.net/ajax/libs/prop-types/15.8.1/prop-types.js"></script>
<script type="text/babel">
// 创建
class Demo extends React.Component {
// 展示左侧输入框的数据
showData = ()=>{
const {input1}=this
alert(input1.value)
}
// 展示右侧输入框的数据
showData2=()=>{
const {input2}=this
alert(input2.value)
}
render(){
return (
<div >
<input ref={(c)=>{this.input1=c}} type = "text" placeholder="点击按钮提示数据"/>
<button ref="button100" type="button" onClick={this.showData}>点击按钮提示左侧数据</button>
<input ref={(c)=>{this.input2=c}} onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/>
</div>
)
}
}
// 渲染
ReactDOM.render(<Demo/>,document.getElementById('test'))
</script>
</body>
</html>
3.回调ref中的回调次数
若ref回调函数是以内联函数的方式定义的,在更新过程中它会被执行两次,第一次传入参数null,第二次传入参数DOM元素。因为每次渲染时会创建一个新的函数实例,所以React清空旧的ref并且设置新的,通过将ref的回调函数定义成class的绑定函数的方式可以避免上述问题,但是大多数情况是无关紧要的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id= 'test'></div>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<!-- 生产环境中不建议使用 -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<!-- 引入prop-types,用于对标签属性进行限制 -->
<!-- <script type="text/javascript" src="..js/prop-types.js"></script> -->
<script src="https://cdn.bootcdn.net/ajax/libs/prop-types/15.8.1/prop-types.js"></script>
<script type="text/babel">
// 创建
class Demo extends React.Component {
// 获取原来状态
state = {isHot:false}
// 更新状态
changeWeather = ()=>{
const {isHot}=this.state
this.setState({isHot:!isHot})
}
saveInput = (c)=>{
this.input1=c;
console.log('@',c)
}
// 展示左侧输入框的数据
showInfo = ()=>{
const {input1}=this
alert(input1.value)
}
render(){
const {isHot}=this.state
return (
<div >
<h2>今天天气很{isHot ?'炎热':'凉爽'}</h2>
{/* <input ref={(c)=>{this.input1=c; console.log('@',c)}} type = "text" placeholder="点击按钮提示数据"/> */}
<input ref={this.saveInput} type = "text" placeholder="点击按钮提示数据"/><br/>
<button ref="button100" type="button" onClick={this.showInfo}>点击按钮提示数据</button>
<button onClick = {this.changeWeather}>点击改变天气状态</button>
</div>
)
}
}
// 渲染
ReactDOM.render(<Demo/>,document.getElementById('test'))
</script>
</body>
</html>
4.createRef 形式的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id= 'test'></div>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<!-- 生产环境中不建议使用 -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<!-- 引入prop-types,用于对标签属性进行限制 -->
<!-- <script type="text/javascript" src="..js/prop-types.js"></script> -->
<script src="https://cdn.bootcdn.net/ajax/libs/prop-types/15.8.1/prop-types.js"></script>
<script type="text/babel">
// 创建
class Demo extends React.Component {
// React.creatRef调用后可以返回一个容器,该容器可以存储被ref所标识的节点,该容器是"专人专用"的
myRef = React.createRef()
myRef2 = React.createRef()
// 展示左侧输入框的数据
showData = ()=>{
// const {input1}=this
alert(this.myRef.current.value)
}
showData2 = ()=>{
// const {input1}=this
alert(this.myRef2.current.value)
}
render(){
return (
<div >
<input ref={this.myRef} type = "text" placeholder="点击按钮提示数据"/>
<button ref="button100" type="button" onClick={this.showData}>点击按钮提示左侧数据</button>
<input ref={this.myRef2} type = "text" placeholder="失去焦点提示数据" onBlur={this.showData2 }/>
</div>
)
}
}
// 渲染
ReactDOM.render(<Demo/>,document.getElementById('test'))
</script>
</body>
</html>