首页 > 解决方案 > 调试为什么 react-native 发布版本会崩溃并调试一个工作正常

问题描述

我在调试模式下工作,一切看起来都很好,但是当我将我的应用程序构建到 TestFlight(即发布版本)时,它在 LaunchScreen 崩溃了。

经过一段时间和调试后,我将问题缩小到以下代码

import React from 'react'
import { Subscribe } from 'unstated'

const getStoreAsProps = (storeArr) => {
  const storeProps = {}
  storeArr.map(value => (storeProps[value.constructor.name] = value))
  return storeProps
}

const withStore = (...args) => (Element) => () => (
  <Subscribe to={[...args]}>{(...args) => <Element {...getStoreAsProps(args)} />}</Subscribe>
)

export default withStore

其工作方式如下

import React from "react"
import { Text } from "react-native"
import AuthStore from "./store/Auth"
import RouterStore from "./store/Router"
import withStore from "./store"

class MyComponent extends React.Component {
  render() {
  const {AuthStore, RouterStore} = this.props
  return <Text>{AuthStore.state.username} {RouterStore.state.pathname}</Text>
  }
}

export default withStore(AuthStore, RouterStore)(MyComponent)

所以本质上withStore是一个高阶组件,它可以接受任意数量的参数,在这种情况下Stores并将它们传递给Subscribe组件(这是我正在使用的状态管理库的一部分,称为unstaded),它反过来返回渲染道具,然后传递给我的组件作为道具。

它在调试模式下工作正常,但在发布模式下出现这样的错误

评估 e.state 时 undefined 不是对象

XCode 调试日志中的此错误。

我认为 Release 构建期间的某处与 Debug 相比有所不同this.props.AuthState,例如 [undefined] 并且当我指定<Text>{AuthStore.state.username} {RouterStore.state.pathname}</Text>我的 Store 道具未定义的位置时抛出错误,因此我无法访问它们的状态。

我很想保留我为商店制作的这个高阶组件,因为它可以提供非常好的开发体验,但需要能够调试发布版本中到底发生了什么,到目前为止我还无法做到这一点。

在发布构建期间进行了哪些优化可能会在这里产生影响?

标签: javascriptiosreactjsreact-native

解决方案


答案来自:https ://github.com/facebook/metro/issues/182#issuecomment-398052619

问题在于访问value.constructor.name我假设它始终与我的Container名字相同的地方。在缩小这些更改期间,因此它是未定义的,向类添加唯一标识符可以解决问题


推荐阅读