gpt4 book ai didi

javascript - 在 ref 属性中使用字符串文字已被弃用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:04 25 4
gpt4 key购买 nike

我在 React native 代码中执行此操作,eslint 显示行 ref="drawer"

错误

[eslint] Using string literals in ref attributes is deprecated.

执行此操作的正确方法是什么。

<DrawerLayoutAndroid
drawerWidth={300}
ref="drawer"
drawerPosition={DrawerLayoutAndroid.positions.Left}
renderNavigationView={() => navigationView}
</DrawerLayoutAndroid>

最佳答案

您需要使用ref 回调模式 作为string refs are deprecated.

<DrawerLayoutAndroid
drawerWidth={300}
ref={(ref) => this.drawer = ref}
drawerPosition={DrawerLayoutAndroid.positions.Left}
renderNavigationView={() => navigationView}
</DrawerLayoutAndroid>

(ref) => this.drawer = ref 是一个箭头函数语法,你可以简化成类似

constructor(props) {
super(props);
this.setRef = this.setRef.bind(this);
}
setRef(ref) {
this.setRef = ref;
}

...
ref={this.setRef}

查看此答案以获取更多信息

How to access a DOM element in React? What is the equilvalent of document.getElementById() in React

关于javascript - 在 ref 属性中使用字符串文字已被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50504358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com