我正在使用此代码通过按一个按钮来显示我的输入值。但是,只要我输入文本,只要在 组件中输入任何输入,它就会自动显示一个警告框。
当我按下“添加用户”按钮时,我想显示警报框。怎么了?
import React, { Component } from 'react';
import {View, Text, TextInput,Alert, Button} from 'react-native';
export default class myTest extends Component{
constructor(){
super();
this.state = {firstName: 'your name', lastName: 'your last', email: 'enter your email'};
}
onPressButton(){
let firstName = this.state.firstName;
let lastName = this.state.lastName;
Alert.alert("You've Entered" + firstName+"\n "+lastName+"\n");
}
render()
{
return (
<View>
<Text>ENTER YOUR DETAILS:</Text>
<TextInput
style={{height: 40}}
placeholder="FirstName"
onChangeText={(firstName) => this.setState({firstName})}
/>
<TextInput
style={{height: 40}}
placeholder="Last Name"
onChangeText={(lastName) => this.setState({lastName})}
/>
<Text style={{padding: 10, fontSize: 30}}>
firstName: {this.state.firstName}{'\n'}
lastName: {this.state.lastName}
</Text>
<Button title="Add User" onPress={this.onPressButton()}/>
</View>
);
}
}
答案 0 :(得分:1)
“内部按钮”标记中,将onPress = {this.onPressButton()}
替换为onPress = {() => this.onPressButton()}