React Native 不变违规:元素类型无效
React Native Invariant Violation: Element type is invalid
我正在使用 React Native 构建一个 iOS 应用程序,我正在尝试显示地图。但是,我 运行 遇到了问题。每当我尝试显示 MapView
元素时,我都会收到以下错误:
Invariant Violation: Element type is invalid: expected a string (for
built-in components) or a class/function (for composite components)
but got: undefined. You likely forgot to export your component from
the file it's defined in.
Check the render method of `Main`.
This error is located at:
in Main (at App.js:69)
in App (at renderApplication.js:35)
in RCTView (at View.js:113)
in View (at AppContainer.js:102)
in RCTView (at View.js:113)
in View (at AppContainer.js:122)
in AppContainer (at renderApplication.js:34)
createFiberFromElementType
ReactNativeFiber-dev.js:974:90
createFiberFromElement
ReactNativeFiber-dev.js:954:20
reconcileSingleElement
ReactNativeFiber-dev.js:1403:26
reconcileChildFibers
ReactNativeFiber-dev.js:1457:40
reconcileChildrenAtPriority
ReactNativeFiber-dev.js:1634:54
reconcileChildren
ReactNativeFiber-dev.js:1631:12
finishClassComponent
ReactNativeFiber-dev.js:1669:12
updateClassComponent
ReactNativeFiber-dev.js:1659:12
beginWork
ReactNativeFiber-dev.js:1786:23
这是我的Main
组件,和React Native网站上MapView documentation展示的一模一样(除了我把showsUserLocation
设置为false
,这不会导致错误):
import React, { Component } from 'react';
import { MapView } from 'react-native';
class Main extends Component {
render() {
return (
<MapView
style={{height: 200, margin: 40}}
showsUserLocation={false}
/>
);
}
}
export default Main;
错误指的是App.js
中的第69行,即render
方法中的return
语句。为了完整起见,这是 App
class:
import React, { Component } from 'react';
import MainIOS from "./ios/Main";
class App extends Component<{}> {
render() {
return (<MainIOS />);
)
}
}
export default App;
我该如何解决这个问题?我不喜欢使用任何第三方包或库,而只使用标准的 React Native 组件。
如 react-native docs for MapView (v0.38)、
中所述
MapView
IMPORTANT: This component is now DEPRECATED and will be removed in January 2017 (React Native version 0.42). This component
only supports iOS.
Please use react-native-maps
by Airbnb instead of this
component. Our friends at Airbnb have done an amazing job building a
cross-platform MapView
component that is more feature complete. It
is used extensively (over 9k installs / month).
我正在使用 React Native 构建一个 iOS 应用程序,我正在尝试显示地图。但是,我 运行 遇到了问题。每当我尝试显示 MapView
元素时,我都会收到以下错误:
Invariant Violation: Element type is invalid: expected a string (for
built-in components) or a class/function (for composite components)
but got: undefined. You likely forgot to export your component from
the file it's defined in.
Check the render method of `Main`.
This error is located at:
in Main (at App.js:69)
in App (at renderApplication.js:35)
in RCTView (at View.js:113)
in View (at AppContainer.js:102)
in RCTView (at View.js:113)
in View (at AppContainer.js:122)
in AppContainer (at renderApplication.js:34)
createFiberFromElementType
ReactNativeFiber-dev.js:974:90
createFiberFromElement
ReactNativeFiber-dev.js:954:20
reconcileSingleElement
ReactNativeFiber-dev.js:1403:26
reconcileChildFibers
ReactNativeFiber-dev.js:1457:40
reconcileChildrenAtPriority
ReactNativeFiber-dev.js:1634:54
reconcileChildren
ReactNativeFiber-dev.js:1631:12
finishClassComponent
ReactNativeFiber-dev.js:1669:12
updateClassComponent
ReactNativeFiber-dev.js:1659:12
beginWork
ReactNativeFiber-dev.js:1786:23
这是我的Main
组件,和React Native网站上MapView documentation展示的一模一样(除了我把showsUserLocation
设置为false
,这不会导致错误):
import React, { Component } from 'react';
import { MapView } from 'react-native';
class Main extends Component {
render() {
return (
<MapView
style={{height: 200, margin: 40}}
showsUserLocation={false}
/>
);
}
}
export default Main;
错误指的是App.js
中的第69行,即render
方法中的return
语句。为了完整起见,这是 App
class:
import React, { Component } from 'react';
import MainIOS from "./ios/Main";
class App extends Component<{}> {
render() {
return (<MainIOS />);
)
}
}
export default App;
我该如何解决这个问题?我不喜欢使用任何第三方包或库,而只使用标准的 React Native 组件。
如 react-native docs for MapView (v0.38)、
中所述MapView
IMPORTANT: This component is now DEPRECATED and will be removed in January 2017 (React Native version 0.42). This component only supports iOS.
Please use
react-native-maps
by Airbnb instead of this component. Our friends at Airbnb have done an amazing job building a cross-platformMapView
component that is more feature complete. It is used extensively (over 9k installs / month).