【发布时间】:2017-12-15 22:20:51
【问题描述】:
我是一名 iOS 开发人员,目前正在开发一个实验性的 React Native 应用程序。
我有以下代码,它在屏幕上显示一个按钮和示例文本。
import React from 'react';
import { StyleSheet, Text, View , Button } from 'react-native';
export default class App extends React.Component {
constructor() {
super();
this.state = {sampleText: 'Initial Text'};
}
changeTextValue = () => {
this.setState({sampleText: 'Changed Text'});
}
_onPressButton() {
<Text onPress = {this.changeTextValue}>
{this.state.sampleText}
</Text>
}
render() {
return (
<View style={styles.container}>
<Text onPress = {this.changeTextValue}>
{this.state.sampleText}
</Text>
<View style={styles.buttonContainer}>
<Button
onPress={this._onPressButton}
title="Change Text!"
color="#00ced1"
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5deb3',
alignItems: 'center',
justifyContent: 'center',
},
buttonContainer: {}
});
上面的代码显示文本和一个按钮。
但是,当我单击按钮时,应用程序崩溃,而不是显示要显示的新文本。
我是 React Native 新手,请指导我如何解决错误。
【问题讨论】:
标签: javascript ios reactjs react-native setstate