react 中 ref 的用法

说明

  • 本文通过ref 使其获得光标来进行展示
  • 使用到了 componentDidMount() 方法: 在第一次渲染后调用,只在客户端
    • 用来执行代码,操作 ref
  • ref 的名字统一命名为 content
  1. 使用 string 的方式直接绑定
  • 这种方法以后会被废弃
<input type="text" ref="content"/>    //绑定
this.refs.content.focus()     //调用

代码展示:

    <script type="text/babel">

        class App extends React.Component {
            componentDidMount(){
                console.log(this.refs)     //打印 ref 看看效果
                this.refs.content.focus()  //获取 ref --- this.refs.xxx
            }
            render(){
                return (
                    <div>
                        <li>你好,这里是 li</li>
                        <input type="text" ref="content"/>
                    </div>
                )
            }
        }
        ReactDOM.render(<App />, document.getElementById("app"))
    </script>

打印 ref 展示

在这里插入图片描述
视图层结果展示
在这里插入图片描述

  1. callback回调函数形式
  • 推荐使用
  • 就是将这个元素 ref的名字绑定到this里面,调用的时候直接this.xxx
 <input type="text" ref={(el)=>this.content = el}/>   //绑定
this.content.focus()   //调用

代码展示:

    <script type="text/babel">

        class App extends React.Component {
            componentDidMount(){
                console.log(this.content)
                this.content.focus()
            }
            render(){
                return (
                    <div>
                        <li>你好,这里是 li</li>
                        <input type="text" ref={(el)=>this.content = el}/>
                    </div>
                )
            }
        }
        ReactDOM.render(<App />, document.getElementById("app"))
    </script>

打印 ref 展示
在这里插入图片描述
视图层结果展示
在这里插入图片描述

  1. createRef 形式(最新的方式)
  • 推荐使用
  • 通过createRef的形式进行ref的元素绑定
this.content = React.createRef()  //声明
<input type="text" ref={this.content}/>  //绑定
this.content.current.focus()   //调用

代码展示:

    <script type="text/babel">

        class App extends React.Component {
            constructor(){
                super()
                this.content = React.createRef()  //通过createRef的形式进行ref的元素绑定
            }
            componentDidMount(){
                console.log(this.content)     
                this.content.current.focus()   //第三步使用 //注意:通过current属性才可以获取到dom组件

            }
            render(){
                return (
                    <div>
                        <li>你好,这里是 li</li>
                        {/*第二步:需要在使用的组件或者dom元素上通过ref指明*/}
                        <input type="text" ref={this.content}/>
                    </div>
                )
            }
        }
        ReactDOM.render(<App />, document.getElementById("app"))
    </script>

打印 ref 展示
在这里插入图片描述
视图层结果展示
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Reactref是一个用来获取组件或DOM元素的返回值的属性。在React的生命周期函数,你可以使用ref来强制组件重新渲染。 使用ref主要用来访问DOM元素,例如输入框、按钮等。使用ref可以实现获取输入框的文本、获取按钮的值等操作。 ref有两种使用方式:string refs和function refs。 string refs是React较早时引入的一种使用方式,现在已经不再推荐使用。使用string refs需要给元素设置ref属性,值为字符串,然后将ref值赋值给一个成员变量。实例如下: ``` class MyComponent extends React.Component { componentDidMount() { console.log(this.inputRef.value); } render() { return( <input type="text" ref={(input) => this.inputRef = input} /> ) } } ``` function refs是现在推荐使用的一种方式,可以更好的控制和管理组件的引用。使用function refs需要将一个函数作为ref的值,这个函数会在组件挂载或卸载时被执行。实例如下: ``` class MyComponent extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); } componentDidMount() { console.log(this.inputRef.current.value); } render() { return ( <input type="text" ref={this.inputRef} /> ) } } ``` 总结而言,ref是一个非常好用的工具,能够让开发人员更加方便的操作DOM元素,并且更好的控制和管理组件的引用。但是,需要注意的是,过度使用ref会使代码变得混乱难以维护,建议谨慎使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值