似乎你正试图从“react-native”包中访问“ReactNative.Component”

我正在尝试从Tyler McGinnis教程中学习反应原生: https : //egghead.io/lessons/react-react-native-basic-ios-routing#/tab-code

下面的错误消息似乎很直接,但我不知道我在做什么错。

似乎你正试图从“react-native”包中访问“ReactNative.Component”。 也许你的意思是从“反应”包中访问“React.Component”。 例如,而不是:

从'react-native'导入React,{Component,View};

你现在应该这样做:

从'react'导入React,{Component}; 从“react-native”导入{View};

import React, { Component } from 'react'; import {AppRegistry,NavigatorIOS,StyleSheet,Text,View} from 'react-native'; import {Main} from './App/Components/Main'; var styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#111111' }, }); 

你错误的原因是:

你的文件将React设置为“react-native”默认导出,而不是 “反应”

在文件的后面,你正在设置为“react-native”默认导出的variables上访问React.Component将不起作用(React native不包含Component作为命名导出)

'react'库是定义了{Component} (因此React.Component )的名为export 的地方


解:

主要更改您的导入到以下内容:

 import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; ... class Main extends React.Component { render(){ return( <View style={styles.mainContainer}> <Text>Testing the Router</Text> </View> ) } }; 

当你现在引用React.Component ,没有问题,因为它已被正确定义,同时仍然保留对React-Native组件的引用。

这个错误消息应该是关于v 0.25.1引入的弃用警告

https://github.com/facebook/react-native/releases/tag/v0.25.1

需要反应本机的React API现在已被弃用。 你仍然在你的Main.js中

 var React = require('react-native'); var { StyleSheet, Text, View } = React; 

将其更改为:

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