react的ref三种用法

第一种 字符串的ref
ref=“myRef” 里面的绑定是字符串的绑定用法,这种ref的形式写多了影响效率问题,官方文档已经不推荐此写法

//创建类式组件
      class Demo extends React.Component {
        //点击按钮
        showData = () => {
          const { input1 } = this.refs;
          alert(input1.value);
        };
        showData2 = () => {
          const { input2 } = this.refs;
          alert(input2.value);
        };
        render() {
          return (
            <div>
              <input type="text" ref="input1" placeholder="点击按钮提示数据" />
              <button onClick={this.showData}>点我提示左侧的数据</button>
              <input
                type="text"
                ref="input2"
                onBlur={this.showData2}
                placeholder="失去焦点提示数据"
              />
            </div>
          );
        }
      }

第二种 回调形式的ref的用法

  //创建类式组件
      class Demo extends React.Component {
        //点击按钮
        showData = () => {
          const { input1 } = this;
          alert(input1.value);
        };
        showData2 = () => {
          const { input2 } = this;
          alert(input2.value);
        };
        render() {
          return (
            <div>
              <input
                type="text"
                ref={(c) => (this.input1 = c)}
                placeholder="点击按钮提示数据"
              />
              <button onClick={this.showData}>点我提示左侧的数据</button>
              <input
                type="text"
                ref={(c) => (this.input2 = c)}
                onBlur={this.showData2}
                placeholder="失去焦点提示数据"
              />
            </div>
          );
        }
      }

**这种回调形式的写法也发现了问题 就是调用次数比较多
在点击更新组件的时候:
首先render函数先将原来的回调清空,赋值为null,然后才重新渲染 所以出现两次执行情况**

虽然不影响使用,单如果想要达到最优的效果可以稍微改进一下写法:
 //创建类式组件
      class Demo extends React.Component {
        //点击按钮
        showData = () => {
          const { input1 } = this;
          alert(input1.value);
        };
        showData2 = () => {
          const { input2 } = this;
          alert(input2.value);
        };
        //回调形式的ref
        input2 = (c) => (this.input2 = c);
        render() {
          return (
            <div>
              <input
                type="text"
                ref={(c) => (this.input1 = c)}
                placeholder="点击按钮提示数据"
              />
              <button onClick={this.showData}>点我提示左侧的数据</button>
              <input
                type="text"
                // {ref={(c) => (this.input2 = c)}}  这种方式的话每次渲染都得重新生在更新的时候出现c返回null情况 虽然不影响使用 但也可以使用以下这种方式改进
                ref={this.input2}
                onBlur={this.showData2}
                placeholder="失去焦点提示数据"
              />
            </div>
          );
        }
      }

第三种 API(React.createdRef())容器的ref的用法— 目前是官方推荐使用最新的方式

export default class App extends Component {
myRef = React.createRef()
//点击获取inut的数据
showData = () => {
console.log(this.myRef.current.value)
}
render() {
	return <div >
	< input type = "text" ref = {this.myRef}/> 
	<button onClick = {this.showData} > 点击显示输入框的数据 < /button> < /div >
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追逐梦想之路_随笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值