前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >(二)回调形式的 ref

(二)回调形式的 ref

作者头像
老怪兽
发布2023-02-22 14:23:56
4760
发布2023-02-22 14:23:56
举报
文章被收录于专栏:老怪兽的前端之旅

# 🍇一、字符串形式的 ref 不被官方推荐使用,后续更新很有可能直接废弃掉

  1. 过时的字符串 ref

# 二、回调形式的 ref

代码语言:javascript
复制
class Demo {
    render() {
        return(
            <div>
                <input ref={() => {console.log('@')}}/>
                
                <input ref={(a) => {console.log(a)}}/>

                <input ref={(a) => {this.input1 = a}}/>
            </div>
        )
    }
}

ReactDOM.render(<Demo/>, document.getElmentById('test'))
  1. 证明 ref={() => {console.log('@')}} 它是一个回调函数 什么是回调函数:1.程序员定义的函数,2.程序员没有调用,3.这个函数执行了
  1. 接收一下回调函数的参数
  1. ref={(a) => {this.input1 = a}} 这串代码的意思就是,当回调执行了的时候,拿到这个节点,然后把这个节点挂载到了实例身上,名字叫 input1

# 案例

代码语言:javascript
复制
class Demo extends React.Component{
    showData = () => {
        let {input1} = this
        alert(input1.value)
    }

    showData2 = () => {
        let {input2} = this
        alert(input1.value)
    }

    render() {
        return (
            <div>
                <input ref={C = this.input1 = C} type="text" placeholder="点击按钮提示数据"/>
                <button onClick="this.showData">点我提示左侧的数据</button>
                <input onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/>
            </div>
        )
    }
}

ReactDOM.render(<Demo/>, document.getElmentByID('test'))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023年1月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # 🍇一、字符串形式的 ref 不被官方推荐使用,后续更新很有可能直接废弃掉
  • # 二、回调形式的 ref
  • # 案例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档