DrawerLayoutAndroid:ref不工作

时间:2016-08-18 15:10:51

标签: react-native react-native-android

我像这样插入DrawerLayoutAndroid:

<DrawerLayoutAndroid
    ref={(_drawer) => {this.drawer = _drawer; console.log('Here');}}
    [..]
>[...]

结果,没有控制台输出,并且未设置this.drawer。看来ref完全被忽略了。

作为另一种方法,我尝试使用ref作为字符串:

<DrawerLayoutAndroid
    ref={'drawer'}
    [..]
>[...]

this.refs稍后不会容器drawer

有什么想法吗?我正在使用react-native 0.30.0

1 个答案:

答案 0 :(得分:0)

我也为ref使用了<DrawerLayoutAndroid>,这可以通过以下配置正常运行:

...

open = () => {
    this._drawer.openDrawer();
}

close = () => {
    this._drawer.closeDrawer();
}

render() {
    return (
      <DrawerLayoutAndroid
        ref={ref => this._drawer = ref}
        drawerPosition={DrawerLayoutAndroid.positions.Left}
        drawerBackgroundColor="rgba(0,0,0,0.3)"
        onDrawerOpen={() => this.setState({open: true})}
        onDrawerClose={() => this.setState({open: false})}
        drawerWidth={300}
        drawerLockMode={this.state.lockMode}
        renderNavigationView={() => this.props.menu}
      >
        ...
      </DrawerLayoutAndroid>
   );
}

...