我仍在为Android和Android做出反应原生开发的第一步iOS和我偶然发现了按钮的问题(据说很简单)。我不知道如何通过按下按钮来修改文本字段。
我尝试使用state
,因此目前的情况如下:
import React, { Component } from 'react';
import {
...
Button
} from 'react-native';
// more code
class ExampleButton extends ParseComponent {
constructor() {
super();
this.state = {
label: 'nothing'
}
}
// more code
render() {
if(...) {
return (
<Button
onPress={onButtonPress}
title="Click_test"
color="#841584"/>
);
}
}
const onButtonPress = () => { this.setState({label: 'something' });
但是,按下
按钮时出现错误信息... unexpected token, expected (....
在const onButtonPress......
行
我非常关注任何提示! :)谢谢。
答案 0 :(得分:1)
class ExampleButton extends ParseComponent {
constructor() {
super();
this.state = {
label: 'nothing'
}
}
// more code
onButtonPress = () => { //Make a property of ExampleButton class
this.setState({label: 'something' });
} //was missing this closing bracket
render() {
if(...) {
return (
<Button
onPress={this.onButtonPress} //change to this.onButtonPress
title="Click_test"
color="#841584"/>
);
}
}