我刚刚开始学习react,所以如果这听起来像个愚蠢的问题,我会提前道歉。我正在尝试创建一个简单的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);
问题是,当我在我的设备上从Xcode运行它时,我得到
Can't find variable: React
render
main.jsbundle:1509:6
mountComponent
mountChildren
我试着在网上搜索答案,但找不到任何真正有帮助的东西。你知道问题出在哪里吗?
发布于 2016-08-01 00:24:08
在最新版本的React Native中,你必须从' React‘包中导入react
import React, {Component} from 'react';
import {
View,
...
} from 'react-native';
发布于 2019-05-07 22:17:21
import * as React from 'react';
发布于 2021-03-13 13:03:53
在渲染前将构造函数添加到
constructor(props) {
super(属性);}
https://stackoverflow.com/questions/38685849
复制相似问题