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>
相关推荐
子兮曰4 小时前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
竹林8185 小时前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花5 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu12276 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪6 小时前
Vue3-生命周期
前端
莪_幻尘6 小时前
你的 AI Skill 越多越蠢?Token 上下文爆炸的求生指南
前端·ai编程
lichenyang4537 小时前
从 has.echo 到异步 API 注册表:一次 ASCF API 回调不触发的排查复盘
前端
林瞅瞅7 小时前
Nuxt3 项目部署 Nginx 防盗链后特定 JS 文件 403 问题修复方案
前端
kyriewen7 小时前
别再每次都 Google 了:我整理了前端日常最常踩的 10 个 Git 坑,附速查表
前端·javascript·git
一颗奇趣蛋8 小时前
Web 视频开发完全指南:从入门到精通
前端