Javascript 找不到变量:反应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38685849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't find variable: React
提问by KeykoYume
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:
我刚刚开始学习反应,所以如果这听起来像一个愚蠢的问题,我提前道歉。我正在尝试使用触发操作的按钮制作一个简单的 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);
The problem is that when I run it from Xcode on my device I get
问题是,当我在我的设备上从 Xcode 运行它时,我得到
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?
我试图在网上搜索答案,但找不到任何真正有帮助的东西。知道这里可能有什么问题吗?
回答by dopcn
In the latest version of React Native you must import React from 'react' package
在最新版本的 React Native 中,您必须从“react”包中导入 React
import React, {Component} from 'react';
import {
View,
...
} from 'react-native';
回答by Elialber Lopes
import * as React from 'react';