如何使用回调函数在ChangeText上填充textinput的值prop



在这段代码中,我所要实现的就是在Textinput的值道具中获得类型化的值。然而,我得到的是空的。setState是异步的,我落后了一步。我是react的新手,我不知道如何制作一个回调函数来给我当前值。

我还希望以后使用map,并制作动态表单,我很感激这个解决方案可能也适用于map函数。

感谢

import React from 'react';
import {
View,
TextInput,
} from 'react-native';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
Email: null,
Password: null,
Address: null,
};
}
EnterValue = (name) => {
return (text) => {
this.setState({
[name]: text,

});
};
};
render() {
return (
<View>
<TextInput
placeholder="enter email"
value={this.state.Email}
onChangeText={this.EnterValue('Email')}
style={{ borderWidth: 2, borderColor: 'skyblue', margin: 20 }}
/>
<TextInput
placeholder="Password"
value={this.state.Password}
onChangeText={this.EnterValue('Password')}
style={{ borderWidth: 2, borderColor: 'skyblue', margin: 20 }}
/>
<TextInput
placeholder="Address"
value={this.state.Address}
onChangeText={this.EnterValue('Address')}
style={{ borderWidth: 2, borderColor: 'skyblue', margin: 20 }}
/>
</View>
);
}
}

我会做一些类似的事情

onChangeText={(e) => this.EnterValue(e.target.value, 'Email')}

(我对e.target.value没有信心,如果它为null或未定义,请传递e.target并记录它,看看你能从中得到什么值(

然后使用以下更改您的功能

EnterValue = (value, inputName) => this.setState({ [inputName]: value});

最新更新