react学习——10react中ref属性

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="点击按钮提示数据"/>&nbsp;
                        <button ref="btn" onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                    <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="点击按钮提示数据"/>&nbsp;
                        <button ref="btn" onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                    <input onBlur={this.showData2} ref={c=>this.input2 = c} type="text" placeholder="点击按钮提示数据"/>&nbsp;
                </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="点击按钮提示数据"/>&nbsp;<br/>*/}
                    <input ref={this.saveInput} type="text" placeholder="点击按钮提示数据"/>&nbsp;<br/>
                    <button ref="btn" onClick={this.showData}>点我提升输入数据</button>
                    &nbsp;
                    <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="点击按钮提示数据"/>&nbsp;
                        <button ref="btn" onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                    <input onBlur={this.showData2} ref={this.myRef2} type="text" placeholder="点击按钮提示数据"/>&nbsp;
                </div>
                )
        }
    }
    ReactDOM.render(<Demo/>,document.getElementById('root'))
</script>
</body>
</html>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶飘零晋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值