当前位置: 首页 > news >正文

React-06React中refs属性(字符串refs,回调形式,React.createRef() )

1.React中refs属性

绑定到render输出的任何组件上,通过this.ref.绑定名直接操作DOM元素或获取子组件的实例。

2.绑定refs实例

2.1 字符串refs(已经过时参考官网API)

字符串(string)的ref存在一定的效率问题

  <input ref='input1' type="text" placeholeder='点击按钮提示数据'/>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello,React</title>
</head>

<body>
    <!-- 容器 -->
    <div id="test"></div>

    <!-- {/* // 引入 React核心库 */} -->
    <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <!-- {/* // 引入 react-dom 用于支持 react 操作 DOM */} -->
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <!-- {/* // 引入 babel:1. ES6 ==> ES5 2. jsx ==> js */} -->
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <!-- {/* // 引入 JSX 语法 */} -->
    <script type="text/babel">
        // 1.创建组件
        class MyComponent extends React.Component {
            render() {
                return (
                    <div>
                        <input ref='input1' type="text" placeholeder='点击按钮提示数据'/>&nbsp;
                        <button ref='button1' onClick={this.showData}>点击提示左侧数据</button>&nbsp;
                        <input ref='input2' onBlur={this.showData2} type="text" placeholeder='失去焦点提示数据'/>
                    </div>
                )
            }
            // 左侧事件处理函数 ref标识使用
            showData = () => {
                console.log(this);
                
                alert(this.refs.input1.value);
            }
            // 右侧事件处理函数 失去焦点触发
            showData2 = () => {
                alert(this.refs.input2.value);
            }
        }
        // 2. 渲染虚拟DOM到页面
        ReactDOM.render(<MyComponent />,document.getElementById('test'))
    </script>
</body>

</html>
2.2 回调形式refs
<input ref={c => this.input1 = c} type="text" placeholeder='点击按钮提示数据'/>

整体代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello,React</title>
</head>

<body>
    <!-- 容器 -->
    <div id="test"></div>

    <!-- {/* // 引入 React核心库 */} -->
    <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <!-- {/* // 引入 react-dom 用于支持 react 操作 DOM */} -->
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <!-- {/* // 引入 babel:1. ES6 ==> ES5 2. jsx ==> js */} -->
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <!-- {/* // 引入 JSX 语法 */} -->
    <script type="text/babel">
        // 1.创建组件
        class MyComponent extends React.Component {
            // 回调函数 形式的ref标识使用
            render() {
                return (
                    <div>
                        <input ref={c => this.input1 = c} type="text" placeholeder='点击按钮提示数据'/>&nbsp;
                        <button onClick={this.showData}>点击提示左侧数据</button>&nbsp;
                        <input ref={c => this.input2 = c} onBlur={this.showData2} type="text" placeholeder='失去焦点提示数据'/>
                    </div>
                )
            }
            // 左侧事件处理函数 ref标识使用
            showData = () => {
                const {input1} =this
                alert(input1.value);
            }
            // 右侧事件处理函数 失去焦点触发
            showData2 = () => {
                const {input2} =this
                alert(input2.value);
            }
        }
        // 2. 渲染虚拟DOM到页面
        ReactDOM.render(<MyComponent />,document.getElementById('test'))
    </script>
</body>

</html>
2.2.1 回调函数refs以内联方式定义,更新过程中会执行两次

        ref回调函数如果以内联函数的方式定义,在更新的过程中会执行两次,第一次传入null,第二次传入参数DOM元素,每次渲染时会创建新的函数实例,所以React清空旧的的ref并设置新的。影响几乎不存在

 整体代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello,React</title>
</head>

<body>
    <!-- 容器 -->
    <div id="test"></div>

    <!-- {/* // 引入 React核心库 */} -->
    <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <!-- {/* // 引入 react-dom 用于支持 react 操作 DOM */} -->
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <!-- {/* // 引入 babel:1. ES6 ==> ES5 2. jsx ==> js */} -->
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <!-- {/* // 引入 JSX 语法 */} -->
    <script type="text/babel">
        // 1.创建组件
        class MyComponent extends React.Component {
            state = {isHot:true}

            // 事件处理函数 ref标识使用
            showData = () => {
                const {input1} =this
                alert(input1.value);
            }
            // 回调函数 形式的ref标识使用
            render() {
                const {isHot} = this.state
                return (
                    <div>
                        <h2>{isHot?'很热':'很冷'}</h2>
                        <button onClick={()=>this.setState({isHot:!isHot})}>点击切换天气</button>&nbsp;
                        <input ref={c => {this.input1 = c;console.log('渲染次数:',c)}} type="text" placeholeder='点击按钮提示数据'/>&nbsp;
                        <button onClick={this.showData}>点击提示左侧数据</button>&nbsp;
                    </div>
                )
            }
        }
        // 2. 渲染虚拟DOM到页面
        ReactDOM.render(<MyComponent />,document.getElementById('test'))
    </script>
</body>

</html>
2.3 React.createRef() 容器存储ref所标识的节点
2.3.1 声明调用
 // 调用后返回容器(声明唯一标识使用),容器存储ref所标识的节点
    myRef = React.createRef()
 showData = () => {
       console.log(this.myRef.current.value);
       alert(this.myRef.current.value);
     }
2.3.2 对应节点绑定 
<input ref={this.myRef} type="text" placeholeder='点击按钮提示数据'/>&nbsp;  
2.3.3 整体代码 
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello,React</title>
</head>

<body>
    <!-- 容器 -->
    <div id="test"></div>
    <!-- {/* // 引入 React核心库 */} -->
    <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <!-- {/* // 引入 react-dom 用于支持 react 操作 DOM */} -->
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <!-- {/* // 引入 babel:1. ES6 ==> ES5 2. jsx ==> js */} -->
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <!-- {/* // 引入 JSX 语法 */} -->
    <script type="text/babel">
        // 1.创建组件
        class MyComponent extends React.Component {
            // 调用后返回容器(声明唯一标识使用),容器存储ref所标识的节点
            myRef = React.createRef()
            myRef2 = React.createRef()
            // 事件处理函数
            showData = () => {
                console.log(this.myRef.current.value);
                alert(this.myRef.current.value);
            }
            // 失去焦点处理函数
            showData2 = () => {
                console.log(this.myRef.current.value);
                alert(this.myRef.current.value);
            }
            render() {
                return (
                    <div>
                        <input ref={this.myRef} type="text" placeholeder='点击按钮提示数据'/>&nbsp;  
                        <button onClick={this.showData}>点击</button>&nbsp;   
                        <input ref={this.myRef2} onBlur={this.showData2}  type="text" placeholeder='点击按钮提示数据'/>&nbsp;          
                    </div>
                )
            }
        }
        // 2. 渲染虚拟DOM到页面
        ReactDOM.render(<MyComponent />,document.getElementById('test'))
    </script>
</body>

</html>

相关文章:

  • JVM考古现场(十七):鸿蒙初辟——从太极二进到混沌原初的编译天道
  • [ctfshow web入门] web24
  • oracle 11g密码长度和复杂度查看与设置
  • Debian系统_主板作为路由器_测试局域网设备间网速
  • 3.Spring-AOP简介/AOP切入点表达式/AOP工作流程(p31-p35)
  • NO.78十六届蓝桥杯备战|数据结构-并查集|双亲表示法|初始化|查询|合并|判断|亲戚|Lake Counting|程序自动分析(C++)
  • [CI]Docker构建时传递CI的唯一生成号作为版本号(SpringBoot、Vue)
  • JVM性能调优:参数配置×内存诊断×GC调优实战
  • vue3中pinia基本使用
  • 前端核心知识:Vue 3 编程的 10 个实用技巧
  • 小程序设计和开发:怎样制定小程序的主要业务流程和功能模块。
  • 优化IntelliJ IDEA 对 Python 的提示功能
  • git配置 gitcode -- windows 系统
  • 云原生周刊:深入探索 kube-scheduler-simulator
  • 实时比分更新系统的搭建
  • zabbix中通过模板实现自动发现对tcp端口批量监控
  • 【区间贪心】合并区间 / 无重叠区间 / 用最少数量的箭引爆气球 / 俄罗斯套娃信封问题
  • 使用在 Google Kubernetes Engine 上运行的 Vertex AI 开始使用 Elastic Chatbot RAG 应用程序
  • opengles,VBO,EBO,VAO啥含义,使用流程
  • Chrome 135 版本开发者工具(DevTools)更新内容
  • 自己网站wordpress主题怎么/网站运营师
  • 东莞市官网网站建设公司/海外短视频跨境电商平台是真的吗
  • 南京机关建设网站/帮平台做推广怎么赚钱
  • 文案策划公司/长春百度seo排名
  • 怎样是做网站/推广任务发布平台app
  • 企业网站创建小结/品牌网络推广