React组件三大核心属性(三)——refs

本文详细介绍了在React中如何使用refs来获取DOM对象,包括字符串方式、回调函数方式和createRef()方法的使用,并对比了它们之间的差异。同时,文章强调了React官方推荐使用createRef()以及事件处理中如何避免过度使用refs,提倡利用event.target获取元素。
摘要由CSDN通过智能技术生成

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


实例

需求:自定义组件,功能说明如下:

  1. 点击按钮,提示第一个输入框中的值。
  2. 当第 2 个输入框失去焦点时,提示这个输入框中的值。

常规做法

我们在使用常规 JS 时,如果想获得 DOM 对象,我们会给 html 标签加上 id,并使用 getElementById 来获取 DOM 对象。

//展示左侧输入框的数据
showData = ()=>{         
                const input1 = document.getElementById('input1')
                alert(input1.value)
            }
//展示右侧输入框的数据
showData2 = ()=>{
                const input2 = document.getElementById('input2')
                alert(input2.value)
            }
render(){
                return (
                    <div>
                        <input id="input1" type="text" placeholder="点击按钮提示数据"/>&nbsp;
                        <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                        <input id="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> 
                    </div>
               )
            }

使用 refs

在 React 中,封装了 refs 来完成获取 DOM 对象的功能。有如下三种方式:

  • 字符串
  • 回调函数
  • createRef()

字符串形式

这种形式虽然很方便,但是使用次数多的话会有效率问题,所以 React 官方不建议使用,今后也有被弃用的可能。

//展示左侧输入框的数据
showData = ()=>{         
                const input1 = this.refs.input1
                alert(input1.value)
            }
//展示右侧输入框的数据
showData2 = ()=>{
                const input2 = this.refs.input2
                alert(input2.value)
            }
render(){
                return (
                    <div>
                        <input ref="input1" type="text" placeholder="点击按钮提示数据"/>&nbsp;
                        <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                        <input ref="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> 
                    </div>
               )
            }

回调函数

回调函数是使用比较多的方式。下面为内联函数的形式。

//展示左侧输入框的数据
showData = ()=>{         
                const input1 = this.input1
                alert(input1.value)
            }
//展示右侧输入框的数据
showData2 = ()=>{
                const input2 = this.input2
                alert(input2.value)
            }
render(){
                return (
                    <div>
                        <input ref={c => {this.input1 = c;}} type="text" placeholder="点击按钮提示数据"/>&nbsp;
                        <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                        <input ref={c => {this.input2 = c;}} onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> 
                    </div>
               )
            }

当然也可以写成类内函数的形式。

//展示左侧输入框的数据
showData = ()=>{         
                const input1 = this.input1
                alert(input1.value)
            }
//展示右侧输入框的数据
showData2 = ()=>{
                const input2 = this.input2
                alert(input2.value)
            }
getinput1 = (c) => {this.input1 = c}
getinput2 = (c) => {this.input2 = c}
render(){
                return (
                    <div>
                        <input ref={this.getinput1} type="text" placeholder="点击按钮提示数据"/>&nbsp;
                        <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                        <input ref={this.getinput2} onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> 
                    </div>
               )
            }

createRef()

这是 React 官方推荐使用的方式,但是每个 createRef() 只能对应一个 DOM 对象,所以如果需要多个 DOM 对象,则需要创建多个 createRef()

//展示左侧输入框的数据
showData = ()=>{         
                const input1 = this.myRef.current
                alert(input1.value)
            }
//展示右侧输入框的数据
showData2 = ()=>{
                const input2 = this.myRef2.current
                alert(input2.value)
            }
// 创建ref容器
myRef = React.createRef()
myRef2 = React.createRef()
render(){
                return (
                    <div>
                        <input ref={this.myRef} type="text" placeholder="点击按钮提示数据"/>&nbsp;
                        <button onClick={this.showData}>点我提示左侧的数据</button>&nbsp;
                        <input ref={this.myRef2} onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> 
                    </div>
               )
            }

事件处理

  1. 通过 onXxx 属性指定事件处理函数(注意大小写)
    1)React 使用的是自定义(合成)事件,而不是使用原生的 DOM 事件 —— 考虑兼容性。
    2) React中的事件是通过事件委托方式处理的(委托给组件最外层的元素) —— 为了高效。
  2. 通过 event.target 得到发生事件的 DOM 元素对象。React 官方提示不要过分使用 refs,在很多情况我们可以使用 event.target 来代替 refs,比如上述实例中失去焦点提示信息的部分。
//展示右侧输入框的数据
showData2 = (event)=>{
	alert(event.target.value);
}

<input onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: React三大属性是props、state和refs。\[1\] props是组件属性,可以通过this.props来访问。它可以用来传递数据给组件,并且可以对属性进行类型限制和必要性限制。可以使用PropTypes来进行属性类型的限制,也可以设置默认属性。\[1\] state是组件的状态,可以通过this.state来访问。它用于存储组件内部的数据,并且可以通过setState方法来更新状态。状态的改变会触发组件的重新渲染。\[2\] refs是用来标识组件内部标签的引用。它可以通过字符串形式或者React.createRef()来定义。通过refs可以获取到标签的真实DOM节点或者组件实例。\[3\] 这三大属性React非常重要,可以帮助我们管理组件的数据和交互。 #### 引用[.reference_title] - *1* [react三大属性](https://blog.csdn.net/weixin_30617797/article/details/102410491)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [React —— React组件三大属性(state,props,ref)](https://blog.csdn.net/Bonsoir777/article/details/127568414)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值