【问题标题】:TextInput value is not updated after changing state?更改状态后 TextInput 值不更新?
【发布时间】:2020-03-26 15:58:43
【问题描述】:
        <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: '',
    };
}

【问题讨论】:

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


    【解决方案1】:

    试试这个:

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

    小吃样品here.

    【讨论】:

    • 我试过你的世博项目,它工作正常。但是我复制了完全相同的代码,但它在我的项目中不起作用。问题是文本被替换回空字符串。
    【解决方案2】:

    shouldComponentUpdate这个方法是匿名调用导致没有更新TextInput

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-04
      • 2016-10-02
      • 2018-07-08
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2019-12-29
      相关资源
      最近更新 更多