【问题标题】:React Native Build Error With Yarn?用 Yarn 反应原生构建错误?
【发布时间】:2017-02-17 20:26:53
【问题描述】:

实际上,我要做的只是将初始化代码从index.android.js / index.ios.js 移至app/index.js 文件。我将其导出并将其导入回index.android.jsindex.ios.js

然后我得到错误。

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

我使用 npm 时没有这个问题。似乎 yarn 的构建过程可能会有所不同,因为每当我在打包程序中保存任何内容时,它都会显示

Bundling 'index.android.js' 但与 iOS 无关。

我的app/index.js 看起来像这样

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View
} from 'react-native';

export default class Snapp extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
      </View>
    );
  }
}

我的index.android.jsindex.ios.js 文件看起来像这样

import Snapp from './app'
import { AppRegistry } from 'react-native';

AppRegistry.registerComponent('Snapp', () => Snapp);

有什么想法吗?

【问题讨论】:

  • 您在安装纱线之间是否使用react native start -- --reset-cache 重新启动了打包程序?
  • 我在我的项目中运行了rm yarn.lock &amp;&amp; rm -rf node_module &amp;&amp; npm install,也运行了react-native start --reset-cache,但仍然出现错误。
  • 确保打包程序已关闭,也可以尝试rm -rf ios/build ios/DerivedData rm -rf node_modules。打开 Xcode 项目并清理(cmd + shift + K)。重新安装模块,使用重置缓存重新启动打包程序,然后运行。
  • 我也试试,但有没有办法卸载纱线?我环顾四周,但什么都看到了。
  • 也会参考有关您如何安装它的文档。

标签: javascript react-native yarnpkg


【解决方案1】:

尝试在与组件相同的 JS 中注册应用程序。

app/index.js

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  AppRegistry
  View
} from 'react-native';

export default class Snapp extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
      </View>
    );
  }
}
AppRegistry.registerComponent('Snapp', () => Snapp);

index.ios.js / index.android.js

require('./app/');

【讨论】:

  • 试过了,但现在它给了我一个Could not get BatchedBridge, make sure your bundle is packaged properly 错误。当我重置回原始代码时,错误消失了。
  • @maxwel 确保在应用程序之后有“/”。要求('./app/');
  • 即使将其更改为./app/,仍然会出现错误
  • @maxwellgover 你在运行打包程序吗?反应原生开始
  • @maxwellgover 太棒了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2018-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多