1、字符串形式的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- 移动端适配-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>1_字符串形式的ref.html</title>
</head>
<body>
<!--准备一个容器-->
<div id="root"></div>
<!--引入react核心库-->
<script type="text/javascript" src="../js/react.development16.2.0.js"></script>
<!--引入react-dom.js库,用于支持react操作DOM-->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!--引入babel,用于jsx的转换jsx-->
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">/*此处一定要写babel*/
class Demo extends React.Component{
//自定义方法-要用赋值语句的形式+箭头函数
showData= ()=>{
console.log("aaa")
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="btn" onClick={this.showData}>点我提示左侧的数据</button>
<input ref="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('root'))
</script>
</body>
</html>
2、回调形式的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- 移动端适配-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2_回调形式的ref.html</title>
</head>
<body>
<!--准备一个容器-->
<div id="root"></div>
<!--引入react核心库-->
<script type="text/javascript" src="../js/react.development16.2.0.js"></script>
<!--引入react-dom.js库,用于支持react操作DOM-->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!--引入babel,用于jsx的转换jsx-->
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">/*此处一定要写babel*/
class Demo extends React.Component{
//自定义方法-要用赋值语句的形式+箭头函数
showData= ()=>{
const {input1}=this
alert(input1.value)
}
showData2=()=>{
const {input2} = this.refs
alert(input2.value)
}
render(){
return(
<div>
<input ref={c=>this.input1 = c} type="text" placeholder="点击按钮提示数据"/>
<button ref="btn" onClick={this.showData}>点我提示左侧的数据</button>
<input onBlur={this.showData2} ref={c=>this.input2 = c} type="text" placeholder="点击按钮提示数据"/>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('root'))
</script>
</body>
</html>
3、回调ref中回调次数问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- 移动端适配-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3_回调ref中回调次数问题.html</title>
</head>
<body>
<!--准备一个容器-->
<div id="root"></div>
<!--引入react核心库-->
<script type="text/javascript" src="../js/react.development16.2.0.js"></script>
<!--引入react-dom.js库,用于支持react操作DOM-->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!--引入babel,用于jsx的转换jsx-->
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">/*此处一定要写babel*/
class Demo extends React.Component{
state={
isHot:true
}
showData = ()=>{
const {input1} = this;
alert(input1.value)
}
changeWeather = ()=>{
const {isHot} = this.state;
this.setState({
isHot:!isHot
})
}
saveInput = (c)=>{
this.input1 = c;
console.log('@', c)
}
render(){
const {isHot} = this.state;
return(
<div>
<h1>今天天气很{isHot ? '炎热' : '凉爽'}</h1>
{/*<input ref={c => {
this.input1 = c;
console.log('@', c)
}} type="text" placeholder="点击按钮提示数据"/> <br/>*/}
<input ref={this.saveInput} type="text" placeholder="点击按钮提示数据"/> <br/>
<button ref="btn" onClick={this.showData}>点我提升输入数据</button>
<br/>
<button onClick={this.changeWeather}>点位切换天气</button>
</div>
)
}
}
ReactDOM.render(<Demo/>, document.getElementById('root'))
</script>
</body>
</html>
4、createRef的使用(官方推荐)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- 移动端适配-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>4_createRef.html</title>
</head>
<body>
<!--准备一个容器-->
<div id="root"></div>
<!--引入react核心库-->
<script type="text/javascript" src="../js/react.development16.3.1.js"></script>
<!--引入react-dom.js库,用于支持react操作DOM-->
<script type="text/javascript" src="../js/react-dom.development16.3.1.js"></script>
<!--引入babel,用于jsx的转换jsx-->
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">/*此处一定要写babel*/
class Demo extends React.Component{
constructor() {
super();
}
//React.createRef()创建一个容器,该容器可以存储被ref所标识的节点
myRef = React.createRef()
myRef2 = React.createRef()
//自定义方法-要用赋值语句的形式+箭头函数
showData= ()=>{
console.log(this.myRef.current.value)
}
showData2=()=>{
console.log(this.myRef2.current.value)
}
render(){
return(
<div>
<input ref={this.myRef} type="text" placeholder="点击按钮提示数据"/>
<button ref="btn" onClick={this.showData}>点我提示左侧的数据</button>
<input onBlur={this.showData2} ref={this.myRef2} type="text" placeholder="点击按钮提示数据"/>
</div>
)
}
}
ReactDOM.render(<Demo/>,document.getElementById('root'))
</script>
</body>
</html>