javascript - 使用 Yarn react native 构建错误?

标签 javascript react-native yarnpkg

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

然后我收到错误。

元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:对象

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

捆绑“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);

有什么想法吗?

最佳答案

尝试在组件的同一个 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/');

关于javascript - 使用 Yarn react native 构建错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42306798/

相关文章:

javascript - 我无法为已加载的 Javascript 分配 ID

javascript - webpack 从多个入口文件导出类

javascript - 多选更改事件句柄

android - 调试 React-Native 应用程序时如何在 Chrome devtools 中查看 React 选项卡?

android 找不到符号 com.google.android.gms.wallet 构建错误

node.js - 生成yarnpkg ENOENT

javascript - jQuery:我需要在单击时选择元素的背景属性

javascript - React Native - 使用异步数据填充 ListView

npm - 是否可以仅忽略一个模块(或注册表)的依赖散列验证?

github - 在 GitHub 操作中缓存 node_modules