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>
相关推荐
小凯在掘金6 小时前
为什么箭头函数不能作为构造函数?
前端·javascript
JustHappy7 小时前
「vConsole MCP🛠️」我让 AI 直接看见任何 H5 的日志和请求帮你 debug
前端·javascript·程序员
codeGoogle7 小时前
Vue3 UIKit 实战:把聊天、会话、主题和移动端适配全部封装好
前端
大龄秃头程序员8 小时前
iOS Method Swizzling 的工程化实践:如何处理多重交换与第三方冲突
前端
liangshanbo12158 小时前
Webpack 文件指纹面试题整理
前端·webpack·node.js
xieter8 小时前
【技术精选】Node.js 核心事件循环机制与异步 I/O 深度剖析 (2026-10-02)
前端·javascript
计算机魔术师8 小时前
Ethan Mollick 谈点与群:智能体自组织为何让管理假设失效
前端
故七月9 小时前
GEO 信源评估体系:如何判断一条网页信源能否被大模型采信
前端·网络·人工智能
liangshanbo12159 小时前
Webpack 常见 Plugin 面试题
前端·webpack·node.js
xxwl5859 小时前
vue3的入门学习
前端·vue.js·学习