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>
相关推荐
kyriewen16 小时前
今年裁了16万技术人——但字节前端岗反而涨了23%
前端·javascript·面试
用户0595401744616 小时前
把 AI Agent 记忆存储验证从 40 分钟手测压到 3 分钟,pytest + Docker 这套组合太顶了
前端·css
stereohomology17 小时前
MS Edge很多事的一点是把地址栏复制的地址自动转标题文本
前端·edge
imaol117 小时前
链表 -- 环链表
java·前端·链表
imaol117 小时前
链表 -- 双向链表
java·前端·链表
东风破_17 小时前
大前端手里的 Next.js:从 #root 到 SEO,一份 HTML 的两种命运
前端·后端
IT_陈寒18 小时前
Vue的v-for不听话?我被这个Key的坑整懵了
前端·人工智能·后端
王林不想说话18 小时前
ES6 到 ES2026 全面进阶指南:新特性、原理、示例与工程落地
前端·javascript·面试
用户435237138149318 小时前
🚀 Vue3 + TypeScript 学习笔记:从泛型 T 到 Promise<T>,终于搞懂了 keyof 和 typeof
前端
跟着群主去吃肉18 小时前
Cinemachine的应用-第三人称视角
前端·unity3d·游戏开发