找不到变量:React [英] Can't find variable: React

查看:94
本文介绍了找不到变量:React的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just started learning react so I apologise in advance if this may sound like a stupid question. I'm trying to make a simple iOS page with a button that triggers an action. I have followed the tutorial on how to get started and this is my index code:

'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);

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

The problem is that when I run it from Xcode on my device I get

Can't find variable: React
render
main.jsbundle:1509:6
mountComponent
mountChildren

我尝试在线搜索答案,但找不到任何实际有用的方法.知道这里可能是什么问题吗?

I tried to search online for the answer but couldn't find anything that actually help. Any idea what might be the problem here?

推荐答案

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

In the latest version of React Native you must import React from 'react' package

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

这篇关于找不到变量:React的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆