React三大组件--ref

1.定义:组件内的标签可以定义ref属性来标识自己。

2.使用ref的三种方法

  • 字符串形式的ref (这个写法会慢慢替换掉,所以尽量不要写字符串形式)
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1_字符串形式的ref</title>
</head>
<body>
    <div id="text"></div>

    <script src="../../js/react.development.js"></script> 
    <script src="../../js/react-dom.development.js"></script> 
    <script src="../../js/babel.min.js"></script> 
    <script type="text/babel">
        class Demo extends React.Component{
            // 左侧
            left = ()=>{
                const input = this.refs.input1
                alert(input.value)
                console.log(this.refs.input1)
            }
            // 右侧
            right = ()=>{
                const input = this.refs.input2
                alert(input.value)
            }
            render(){
                return (
                    <div>
                        <input ref="input1" type="text" placeholder="点击确定弹框" />
                        <button onClick={this.left}>确定</button>
                        <input ref="input2" onBlur={this.right} type="text" placeholder="失去焦点弹框" />
                    </div>

                )
            }
           
        }
        ReactDOM.render(<Demo />,document.getElementById('text'))
    </script>
</body>
</html>
  • 回调形式的ref(主推用这个形式写)
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2_回调形式的ref</title>
</head>
<body>
    <div id="text"></div>

    <script src="../../js/react.development.js"></script> 
    <script src="../../js/react-dom.development.js"></script> 
    <script src="../../js/babel.min.js"></script> 
    <script type="text/babel">
        class Demo extends React.Component{
            // 左侧
            left = ()=>{
                console.log('@',this)
                const input = this.input1
                alert(input.value)
                console.log(this)
            }
            // 右侧
            right = ()=>{
                const input = this.input2
                alert(input.value)
            }
            render(){
                return (
                    <div>
                        <input ref={(c)=>{this.input1 = c}} type="text" placeholder="点击确定弹框" />
                        <button onClick={this.left}>确定</button>
                        <input ref={(c)=>{this.input2 = c}} onBlur={this.right} type="text" placeholder="失去焦点弹框" />
                    </div>

                )
            }
           
        }
        ReactDOM.render(<Demo />,document.getElementById('text'))
    </script>
</body>
</html>
  • createRef 创建 ref 容器(这个相对来说比较麻烦一些

    html 复制代码
    <div id="text"></div>
    <script src="../../js/react.development.js"></script> 
    <script src="../../js/react-dom.development.js"></script> 
    <script src="../../js/babel.min.js"></script> 
    <script type="text/babel">
        class Login extends React.Component {
            createRef1= React.createRef()
            createRef2= React.createRef()
            left= ()=>{
                const input = this.createRef1
                alter(input.value)
            }
            left= ()=>{
                const input = this.createRef2
                alter(input.value)
            }
            render(){
                //这样写是把username和password 放在了login中,如果还是很迷糊不知道在那的话,可以在left函数中输出this
                return (
                    <div>
                            <input ref={this.createRef1} type="text" placeholder="点击确定弹框" />
                            <button onClick={this.left}>确定</button>
                            <input ref={this.createRef2} onBlur={this.right} type="text" placeholder="失去焦点弹框" />
                        </div>
                )
            }
        }
        ReactDOM.render(<Login />,document.getElementById('text'))
    </script>
相关推荐
福狼5 分钟前
The Expanse:让风沙成为世界的一部分
前端·three.js
程序员鱼皮10 分钟前
这 3 年 AI Agent 到底进化了啥?万字大白话,一次讲透!
前端·后端·aigc
用户9003050936214 分钟前
Claude Code平替评测:换掉之前先理清楚这些选型条件
前端
bai27317 分钟前
同一个 JSON,Node 读出了三种坏法:Windows 上四种写法的编码实测
前端
ZANE17 分钟前
纯前端实现的 .gitignore 生成器技术实现
前端
小y的笔记26 分钟前
Vue 3 异步搜索实战:watch、请求取消与竞态控制
前端·javascript
sanxing三省1 小时前
DeepSeek Harness 到底是什么?标题里最重的词其实不是「DeepSeek」
前端·人工智能·deepseek
jianqiang.xue1 小时前
把Agent塞进MCU:工具次数和上下文长度怎么卡
java·前端·网络·单片机·esp32·ai编程
前端小万2 小时前
思考,行动,复盘|2026
前端·后端·面试
拖孩2 小时前
# 我又用 AI 做了个小程序:24 天,记录我这辈子认识的人
前端·后端·微信小程序