React native 为什么我需要导出组件以将其传递给registerComponent方法?
此代码工作正常:React native 为什么我需要导出组件以将其传递给registerComponent方法?,react-native,ecmascript-6,React Native,Ecmascript 6,此代码工作正常: import React, { Component } from 'react'; import { Text, AppRegistry } from 'react-native'; export default class App extends Component { render() { return ( <Text>Hello World!</Text> ); } } AppRegistry.registe
import React, { Component } from 'react';
import { Text, AppRegistry } from 'react-native';
export default class App extends Component {
render() {
return (
<Text>Hello World!</Text>
);
}
}
AppRegistry.registerComponent('SampleApp', () => App);
需要导出某些内容,以便稍后由其他脚本导入 根据MDN: export语句用于创建JavaScript模块,从模块中导出函数、对象或原语值,以便其他程序使用import语句
我猜registerComponent方法需要一个模块作为参数之一。您遇到了哪一个错误?您是如何导入模块的?还要将
'SampleApp'
修复到'SampleApp'
中我将错误消息添加到我的帖子中。此外,打字错误是固定的。
import React, { Component } from 'react';
import { Text, AppRegistry } from 'react-native';
class App extends Component {
render() {
return (
<Text>Hello World!</Text>
);
}
}
AppRegistry.registerComponent('SampleApp', () => App);
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `ExpoRootComponent`.
This error is located at:
in RootErrorBoundary (at registerRootComponent.js:34)
in ExpoRootComponent (at renderApplication.js:33)
in RCTView (at View.js:60)
in View (at AppContainer.js:102)
in RCTView (at View.js:60)
in View (at AppContainer.js:122)
in AppContainer (at renderApplication.js:32)
throwOnInvalidElementType
ReactNativeRenderer-dev.js:4705:2
createFiberFromElement
ReactNativeRenderer-dev.js:4663:16
reconcileSingleElement
ReactNativeRenderer-dev.js:8337:22
reconcileChildFibers
ReactNativeRenderer-dev.js:8421:12
reconcileChildrenAtExpirationTime
ReactNativeRenderer-dev.js:8621:29
finishClassComponent
ReactNativeRenderer-dev.js:8839:4
updateClassComponent
ReactNativeRenderer-dev.js:8761:11
beginWork
ReactNativeRenderer-dev.js:9580:15
performUnitOfWork
ReactNativeRenderer-dev.js:12924:15