最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using string literals in ref attributes is deprecated - Stack Overflow

programmeradmin8浏览0评论

I am doing this in react native code, eslint is showing error for line ref="drawer"

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

What is correct way to do this.

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

I am doing this in react native code, eslint is showing error for line ref="drawer"

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

What is correct way to do this.

<DrawerLayoutAndroid
                drawerWidth={300}
                ref="drawer"
                drawerPosition={DrawerLayoutAndroid.positions.Left}
                renderNavigationView={() => navigationView}
            </DrawerLayoutAndroid>
Share Improve this question edited May 24, 2018 at 8:21 Jonathan Hall 79.7k19 gold badges159 silver badges203 bronze badges asked May 24, 2018 at 8:19 Khemraj SharmaKhemraj Sharma 59k28 gold badges213 silver badges226 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 19

You need to make use of ref callback pattern as string refs are deprecated.

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

(ref) => this.drawer = ref is an arrow function syntax, you can simplify it similar to

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

...
ref={this.setRef}

Check this answer for more info

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

you can wrap them with curly braces to indicate it's valid JS syntax

<DrawerLayoutAndroid
            drawerWidth={300}
            ref={'DRAWER'}
            drawerPosition={DrawerLayoutAndroid.positions.Left}
            renderNavigationView={() => navigationView}
        </DrawerLayoutAndroid>
发布评论

评论列表(0)

  1. 暂无评论