react学习——10react中ref属性

1、字符串形式的ref

javascript 复制代码
<!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

javascript 复制代码
<!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中回调次数问题

javascript 复制代码
<!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的使用(官方推荐)

javascript 复制代码
<!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>
相关推荐
diang12 分钟前
DeepSeek在前端的使用场景及使用
前端·deepseek
Georgewu15 分钟前
【HarmonyOS Next】鸿蒙应用弹框和提示气泡详解(一)
前端·华为·harmonyos
双口馋猫17 分钟前
cesium+vite demo
前端·vue.js
雾岛听风来18 分钟前
Cython与CUDA之Add
前端·cython
摆烂工程师27 分钟前
什么是MCP?一分钟搞懂!
前端·后端·程序员
A死灵圣法师36 分钟前
同一个接口,掉n次,取消上次请求
前端
前端涂涂40 分钟前
JavaScript面试宝典
前端·javascript
卖报的小行家_1 小时前
读《Vue.js设计与实现》第四章·响应系统的作用与实现
前端
七月丶1 小时前
🚀 前端缓存踩坑指南:如何优雅地解决浏览器缓存问题?
前端
沉默王二1 小时前
更快更强!字节满血版DeepSeek在IDEA中真的爽!
java·前端·程序员