我正在尝试创建一个更改图钉屏幕,但我无法比较从用户那里获得的两个变量(新图钉和确认图钉)。该错误显示“ this.state.newpin”是未定义的对象。
class SettingScreen extends Component {
state = {
oldpin: '000000',
newpin: '',
secpin: ''
}
onPressButton(){
if( this.state.newpin == this.state.secpin){
ToastAndroid.show("Password Changed", ToastAndroid.SHORT);
this.setState({ oldpin : this.state.newpin})
}
else {
ToastAndroid.show("Password Unmatched", ToastAndroid.SHORT);
}
}
handleNewPin = (text) => {
this.setState({ newpin: text })
}
handleSecPin = (text) => {
this.setState({ secpin: text })
}
...
<TextInput onChangeText = {this.handleNewPin} />
<TextInput onChangeText = {this.handleSecPin} />
<TouchableOpacity onPress={this.onPressButton}>
<Text> Change Password </Text>
</TouchableOpacity>
我可以从用户那里获取“ this.state.newpin”和“ this.state.secpin”的输出。 我只是在比较语句(OnPressButton())中失败了。
我是React-Native的新手。
很抱歉给您带来不便。
答案 0 :(得分:1)
您只需要绑定onPressButton()
功能。在构造函数中与此。并像这样将您的状态移至构造函数;
class SettingScreen extends Component {
constructor(props) {
super(props);
this.state = {
oldpin: '000000',
newpin: '',
secpin: ''
};
this.onPressButton = this.onPressButton.bind(this);
}
}