javascript - 调试为什么 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 道具未定义的位置时抛出错误,因此我无法访问它们的状态。
我很想保留我为商店制作的这个高阶组件,因为它可以提供非常好的开发体验,但需要能够调试发布版本中到底发生了什么,到目前为止我还无法做到这一点。
在发布构建期间进行了哪些优化可能会在这里产生影响?
解决方案
答案来自:https ://github.com/facebook/metro/issues/182#issuecomment-398052619
问题在于访问value.constructor.name
我假设它始终与我的Container
名字相同的地方。在缩小这些更改期间,因此它是未定义的,向类添加唯一标识符可以解决问题
推荐阅读
- java - 如何从字符串中获取数字值?
- eclipse - 为什么 Integer.parseInt(args[0]) 在 Eclispse 上不起作用,但此代码在控制台上起作用?
- python - 使用正则表达式理解慢列表
- ruby-on-rails - 我可以使用#to_signed_global_id 获得稳定的签名ID吗?
- numpy - 在单个图形上绘制像素的平均值
- apache-spark - df.count() 和 df.show() 之间的逻辑/物理实施计划差异
- javascript - QGIS 2 web 插件和形状绘图选项
- r - 如何根据 R 中的另一列粘贴行?
- r - 几列的唯一值(基本问题)
- python - 在 django 中查询多对多字段会产生一个空查询集