设置状态 [反应本机] 后无法编辑输入文本

Can't edit inputText after setting state [react-native]

本文关键字:编辑 输入 文本 状态 本机 设置      更新时间:2023-09-26

发送获取请求后,并保存在状态数据中,并显示保存在inputText中的值 我无法再编辑它了。否则,当我的输入清晰时,它可以工作。

这是代码:

第一步我获取提交文本后所需的数据输入input.js

<TextInput
    style={[ style.itemInfoMajor, global.alignRight ]}
    onFocus={ onFocus }
    onChangeText={ this._onChange }
    onSubmitEditing={ () => this._onSubmit(network) }
    value={ value }
    returnKeyType="done"
    enablesReturnKeyAutomatically={ true }
    clearButtonMode="always"
    placeholder={ network }
    placeholderTextColor="rgba(255, 255, 255, 0.25)"
    autoCorrect={ false }
    autoCapitalize="none"
    clearTextOnFocus={ true }
/>

list.js我在加载应用程序后检索数据。

在那里我设置了获得inputText值的状态

componentWillMount() {
  this._loadInitialState().done();
},

async _loadInitialState() {
  try {
    const value = await Storage.get('userData');
    if (value !== null) {
      this.setState({
        data: value,
      });
    }
  }
}
下面是

我将状态传递给输入组件的地方

<Input
  onFocus={ this._handleFocus.bind(this, item) }
  value={ value }
  ref={ item }
  key={ i }
  network={ item }
/>

我加入一个小视频来现场观看问题:https://vid.me/8L9k源代码在这里: https://github.com/statiks/statiks-react-native/blob/master/sources/input/index.js

如何编辑此输入的此值?

尝试在getInitialState中设置值:

getInitialState() {
  return {
    ...,
    value: this.props.value
  };
},  

然后,在 textInput 中使用 this.state.value:

<TextInput
  ...
  value={ this.state.value }
  ... />

然后,更新更改时的值状态:

_onChange(username) {
  this.setState({ value: username });
 },

看起来您可能将不正确的值作为道具向下传递。你需要传递 this.state.data 而不是 value:

<Input
 value={ this.state.data }
 ref={ item }
 key={ i }
 network={ item }
/>

我没有你完整的代码,但我测试了它,它似乎以这种方式为我工作。(您可能需要将父级上数据的初始状态设置为空字符串或数据结构)

我认为_onChange中存在错误。不应该是

_onChange(username) {
    this.setState({ username: username });
},