更改状态后,TextInput值是否未更新?

时间:2020-03-26 15:58:43

标签: android react-native state react-native-textinput

        <TextInput
            label={'Please enter your email'}
            onChangeText={text => this.onChangeText(text)}
            style={textInputStyle}
            value={this.state.testEmail}
        />

检查我用于处理文本的功能,我在其中添加了一些虚拟文本,但没有得到体现。

onChangeText = (text, id) => {
    this.setState((previousState) => ({
                testEmail:
                text+'test'
            }
        )
    )
}

这里是构造函数,

constructor(props) {
    super(props);
    this.state = {
        testEmail: '',
    };
}

2 个答案:

答案 0 :(得分:1)

尝试一下:

<TextInput
  label={'Please enter your email'}
  onChangeText={this.onChangeText}
  style={textInputStyle}
  value={this.state.testEmail}
/>

onChangeText = text => {
  this.setState({
    testEmail: `${text}test`
  });
}

样品小吃here

答案 1 :(得分:0)

shouldComponentUpdate此方法正在匿名调用,这导致不更新TextInput