如何回调函数填充textinput onChangeText的值prop

在这段代码中,我想要实现的就是在Textinput的value prop中获得类型化的值。但是,我得到的是null。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>
    );
  }
}

转载请注明出处:http://www.dlaisen.com/article/20230526/1688448.html