实际上我想做的就是将初始化代码从 index.android.js/index.ios.js
移动到 app/index.js
文件。我将其导出并将其导入回 index.android.js
或 index.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.js
和 index.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/