找不到变量:React

Key*_*ume 25 javascript ios react-native

我刚刚开始学习反应所以我提前道歉,如果这听起来像是一个愚蠢的问题.我正在尝试创建一个简单的iOS页面,其中包含一个触发操作的按钮.我已经按照如何入门的教程,这是我的索引代码:

'use strict';
var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  Component,
  AlertIOS // Thanks Kent!
} = React;

class myProj extends Component {
 render() {
    return (
      <View style={styles.container}>
        <Text>
          Welcome to React Native!
        </Text>
        <TouchableHighlight style={styles.button}
            onPress={this.showAlert}>
            <Text style={styles.buttonText}>Go</Text>
          </TouchableHighlight>
      </View>
    );
  }

  showAlert() {
    AlertIOS.alert('Awesome Alert', 'This is my first React Native alert.', [{text: 'Thanks'}] )
  }
}

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF'
  },
  buttonText: {
    fontSize: 18,
    color: 'white',
    alignSelf: 'center'
  },
  button: {
    height: 44,
    flexDirection: 'row',
    backgroundColor: '#48BBEC',
    alignSelf: 'stretch',
    justifyContent: 'center'
  }
});

AppRegistry.registerComponent('myProj', () => myProj);
Run Code Online (Sandbox Code Playgroud)

问题是,当我从设备上的Xcode运行它时,我得到了

Can't find variable: React
render
main.jsbundle:1509:6
mountComponent
mountChildren
Run Code Online (Sandbox Code Playgroud)

我试图在网上搜索答案,但找不到任何真正有用的东西.知道这可能是什么问题吗?

dop*_*pcn 69

在最新版本的React Native中,您必须从'react'包中导入React

import React, {Component} from 'react';
import {
View,
...
} from 'react-native';
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!只是添加它应该是使用jsx的地方 (2认同)
  • 我必须在每个文件中执行此操作吗? (2认同)

Eli*_*pes 6

import * as React from 'react';
Run Code Online (Sandbox Code Playgroud)