按下按钮时如何更改文本值 React Native?
我开发人员 iOS 目前我在实验申请工作 React Native.
我有以下代码,显示屏幕上的按钮和示例文本。
上面的代码显示文本和按钮。
但是,当我按一个按钮时,应用程序失败,而不是显示要显示的新文本。
我是新的b。 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 color="#00ced1" onpress="{this._onPressButton}" title="Change Text!"></button>
</view>
</view>
/;
}
}
const styles = StyleSheet.create/{
container: {
flex: 1,
backgroundColor: '#f5deb3',
alignItems: 'center',
justifyContent: 'center',
},
buttonContainer: {}
}/;
上面的代码显示文本和按钮。
但是,当我按一个按钮时,应用程序失败,而不是显示要显示的新文本。
我是新的b。 React Native, 请向我解释如何解决此错误。
没有找到相关结果
已邀请:
6 个回复
石油百科
赞同来自:
窦买办
赞同来自:
石油百科
赞同来自:
八刀丁二
赞同来自:
- 这是应该转移到的 onPress 你的按钮。
三叔
赞同来自:
冰洋
赞同来自:
做。 _onPressButton//. 你可以简单
setState//
在这个功能中,没有其他事情可以帮助你解决问题。 如果你想做一些新的,你必须添加
return/ /
并包装文本。