【问题标题】:React Native ScrollView TypeError: undefined is not an object (evaluating 'this._subscribableSubscriptions.forEach')React Native ScrollView TypeError:未定义不是对象(评估'this._subscribleSubscriptions.forEach')
【发布时间】:2017-12-25 22:09:10
【问题描述】:

我正在使用 Expo 构建 React Native 应用程序。它通过 Expo 应用程序在我的 Android 设备上运行良好。但是在我通过 exp build:android 命令构建 apk 后出现错误。

TypeError: undefined is not an object (evaluating 'this._subscribableSubscriptions.forEach')                                                  
This error is located at:
  in ScrollView
  in RCTView
  in r
  in Connect(r)
  in n
  in t
  in r
  in RCTView
  in RCTView
  in t

问题出在 ScrollView 内部。如果我删除 ScrollView,它就消失了。这是我的代码 sn-p。

class Main extends Component {
state = {
    refreshing: false
};

renderCurrencies() {
    if (!Object.values(this.props.currencies).length) {
        return <View />;
    }

    return Object.values(this.props.currencies).map(item => {
        return (
            <CurrencyRow
                key={item.code}
                code={item.code}
                title={item.title}
            />
        );
    });
}

onRefresh = () => {
    Object.values(this.props.currencies).map(item => {
        this.props.sellBuyFetch(item.code);
    });
};

render() {
    return (
        <View style={styles.container}>
            <ScrollView
                refreshControl={
                    <RefreshControl
                        refreshing={this.state.refreshing}
                        onRefresh={this.onRefresh}
                    />
                }
            >
                {this.renderCurrencies()}
            </ScrollView>
        </View>
    );
}
}

const styles = StyleSheet.create({
container: {
    flex: 1,
    marginTop: 40,
},
});

【问题讨论】:

    标签: react-native react-native-android expo


    【解决方案1】:

    此错误是由构建发布版本时使用的uglify-es 3.3.X 引起的。

    将此块添加到您的 package.json:

    "resolutions": { "uglify-es": "3.2.2" }

    我尝试在 Expo 上发布并构建一个独立的应用程序,现在它的工作就像一个魅力。

    【讨论】:

    • 我在 github 上看到了这个解决方案,但不知何故它对我没有帮助。有没有我应该清除的缓存或类似的东西?
    • 检查你的项目,node_modules\uglify-es\package.json,检查它的版本。如果还是3.3.X版本,可以删除uglify-es文件夹,再次运行yarn,然后在XDE中,可以点击Help->Clear XDE Cache
    【解决方案2】:

    有同样的问题。 解决方法:

    改变

    this._subscribableSubscriptions.forEach(
    

    this._subscribableSubscriptions && this._subscribableSubscriptions.forEach(
    

    在那个文件中:

    YOUR_PROJECT/node_modules/react-native/Libraries/Components/Subscribable.js 
    

    【讨论】:

    • @kykapetak 你在 Expo 上试过了吗?我认为它用他们这边的节点模块构建 APK,所以不要帮助我。
    • @KyKaPeTuk 的代码更改为我解决了这个问题。我没有使用 CRNA 或 Expo(出于这些原因)。 react-native Github reponsitory 上有一个帖子:github.com/facebook/react-native/issues/17348
    【解决方案3】:

    谢谢@kykapetuk 和@chillypenguin。看起来像这个 React Native 错误https://github.com/facebook/react-native/issues/17348。如果使用 Expo,我们不能使用 @kykapetuk 解决方法,因为 APK 会在 Expo 服务器上构建。我目前的解决方法 - 不要使用 Expo。可能,这里可能的解决方案是使用另一个版本的 react-native 库。

    【讨论】:

      【解决方案4】:

      我认为你的代码使用 FlatList 会简单得多,但是你的问题可能在这个方法中:renderCurrencies()

      试着改成这样:

      renderCurrencies = () => {

      也许没有找到道具。您在日志中看到任何其他问题吗?如果这没有帮助,请调试问题并查找(this)是否在其中包含该方法。

      注意:我还没有与 Expo 合作过。

      【讨论】:

      • 我试过从 renderCurrencies 方法返回 ,但没有运气。所以问题不在这个方法之内。也试过 FlatList(感谢提示),但有同样的错误。
      【解决方案5】:

      可能是 React Native 0.5.1 中的一个错误。

      我通过在 react native 文件 project/node_modules/react-native/Libraries/Components/Subscribable.js 的第 33 行评估变量 this._subscribableSubscriptions 来修复它

      改变这个:

      this._subscribableSubscriptions.forEach(
      

      到这里:

      this._subscribableSubscriptions && this._subscribableSubscriptions.forEach(
      

      https://github.com/facebook/react-native/issues/17348

      【讨论】:

      • node_modules 中编辑文件绝不是一个好主意,因为这将被库的下一次更新覆盖
      • @NicoHaase react-native 0.55 合并了这个。
      猜你喜欢
      • 2019-04-05
      • 2021-11-09
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2020-09-24
      相关资源
      最近更新 更多