React学习(五)React组件属性之三refs

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="点击按钮提示数据"/>&nbsp;
                        <button ref="button100" type="button" onClick={this.showData}>点击按钮提示左侧数据</button>&nbsp;
                        <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="点击按钮提示数据"/>&nbsp;
                        <button ref="button100" type="button" onClick={this.showData}>点击按钮提示左侧数据</button>&nbsp;
                        <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="点击按钮提示数据"/>&nbsp;*/}
                        <input ref={this.saveInput} type = "text" placeholder="点击按钮提示数据"/><br/>
                        <button ref="button100" type="button" onClick={this.showInfo}>点击按钮提示数据</button>&nbsp;
                        <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="点击按钮提示数据"/>&nbsp;
                        <button ref="button100" type="button" onClick={this.showData}>点击按钮提示左侧数据</button>&nbsp;
                        <input ref={this.myRef2} type = "text" placeholder="失去焦点提示数据" onBlur={this.showData2 }/>&nbsp;
                    </div>
                )

            }
        }
        // 渲染
        ReactDOM.render(<Demo/>,document.getElementById('test'))
    </script>
</body>
</html>
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值