首页 > 解决方案 > React native App 在发布模式下崩溃,并出现以下错误 null is not an object (evalating 's.drawer._root')

问题描述

我使用了本机基础抽屉,它在调试模式下工作正常,但是当我创建发布 apk 时,应用程序因以下错误而崩溃。

AndroidRuntime: com.facebook.react.common.JavascriptException: null is 
not an object (evaluating 's.drawer._root')

代码:

 closeDrawer = () => {
 this.drawer._root && this.drawer._root.close();
};

openDrawer = () => {
this.drawer._root && this.drawer._root.open();
};

<Drawer
      ref={(ref) => {
        this.drawer = ref;
      }}
      type="overlay"
      side={'left'}
      openDrawerOffset={0.2}
      panOpenMask={0.2}
      tapToClose={true}
      content={
        <SideBar
          navigator={this.navigator}
          closeDrawer={() => this.closeDrawer()}
          {...this.props}
        />
      }
      tweenHandler={(ratio) => ({
        main: { opacity: (2 - ratio) / 2 }
      })}
      onClose={() => this.closeDrawer()}
    >

标签: androidreact-nativenative-base

解决方案


看一下关于 ref的react 文档

如果 ref 回调被定义为内联函数,它将在更新期间被调用两次,第一次使用 null,然后再次使用 DOM 元素

在您的 closeDrawer 和 openDrawer 回调中, this.drawer 可能为空,也许您应该添加一些代码,例如

this.drawer && this.drawer._root && this.drawer._root.close();
this.drawer && this.drawer._root && this.drawer._root.open();

推荐阅读