Javascript Can';找不到变量React-即使它未在文件中使用

Javascript Can';找不到变量React-即使它未在文件中使用,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我的React原生项目中出现“找不到变量:React”错误。但是,让我困惑的是,我根本没有在该文件中使用React,尽管我正在导入一个使用它的自定义组件。下面是我的问题的一个例子 首先是construction.js: import React from 'react'; import { View, Text } from 'react-native'; class UnderConstruction extends React.Component { render() {

我的React原生项目中出现“找不到变量:React”错误。但是,让我困惑的是,我根本没有在该文件中使用
React
,尽管我正在导入一个使用它的自定义组件。下面是我的问题的一个例子

首先是
construction.js

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

class UnderConstruction extends React.Component {
    render() {
        return (
            <View>
                <Text style={{ padding: 75 }}>
                    This page is under construction :(
                </Text>
            </View>
        );
    }
}

export default UnderConstruction;
import UnderConstruction from './construction';

export function render() {
    return (
        <UnderConstruction />
    );
}
import React from 'react';
import { AppRegistry } from 'react-native';
import * as Factory from './render';

class Demo extends React.Component {
    render() {
        return Factory.render();
    }
}

AppRegistry.registerComponent('Demo', () => Demo);
最后,
index.ios.js

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

class UnderConstruction extends React.Component {
    render() {
        return (
            <View>
                <Text style={{ padding: 75 }}>
                    This page is under construction :(
                </Text>
            </View>
        );
    }
}

export default UnderConstruction;
import UnderConstruction from './construction';

export function render() {
    return (
        <UnderConstruction />
    );
}
import React from 'react';
import { AppRegistry } from 'react-native';
import * as Factory from './render';

class Demo extends React.Component {
    render() {
        return Factory.render();
    }
}

AppRegistry.registerComponent('Demo', () => Demo);

运行此应用程序将导致在
render.js
行号
5
上出现错误
找不到变量:React
,即:

<UnderConstruction />

我很好奇,即使我没有在
render.js
中使用它,为什么我要导入
反应
,因此我提出了这个问题。那么,是什么原因导致
找不到变量:React
在我的
render.js
文件中出错?

要使用渲染函数,您需要在文件中导入React,因为React创建了元素。我建议您检查一下传输的Javascript文件,您将理解我的意思

我自己也曾面临过这个问题,当我看到传输的JS文件时,我看到了它是如何工作的

因此,在您传输的文件中,它将类似于以下内容:-

(0, _reactDom.render)(_react2.default.createElement(_Root2.default, { store: store, history: history }), document.getElementById('app'));

您是否执行了
npm i--save react
?@Cherniv我使用
react native init Demo
和我的
包创建了该应用程序。json
既有
“react”:“^15.3.2”、
又有
“react native”:“0.33.0”,
我如何查看传输的JS文件?你是否使用babel的webpack?我使用
react native init Demo
和我的
包创建了这个应用程序。json
只有
“react”:“^15.3.2”
“react native”:“0.33.0”
作为依赖项。看起来,一旦你编译,你会得到一个捆绑的输出,对吗?我说的是outfile js filefind:
http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false
。感谢您的洞察力:)