简体   繁体   English

设置状态[react-native]后无法编辑inputText

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

After sending a fetch request, and save in a state data, and display the value saved in the inputText I can't edit it anymore. 发送获取请求后,保存在状态数据中,并显示保存在inputText中的值,我无法再编辑它。 Otherwise, it works when my input is clear. 否则,当我的输入清晰时它会起作用。

Here is the code : 这是代码:

First step I fetch data needed after submiting textInput on input.js 第一步,我在input.js上提交textInput后获取所需的数据

<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 }
/>

On list.js I retrieve data after loading application. list.js我在加载应用程序后检索数据。

It there where I set the state who get the value for the inputText 在那里我设置了获取inputText值的状态

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


async _loadInitialState() {
  try {
    const value = await Storage.get('userData');

    if (value !== null) {
      this.setState({
        data: value,
      });
    }
  }
}

And it's the below where I passed the state to the input component 下面是我将状态传递给输入组件的地方

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

I join a little video to watch in live the problem : https://vid.me/8L9k The source code is here : https://github.com/statiks/statiks-react-native/blob/master/sources/input/index.js 我加入了一个小视频来实时观看问题: https//vid.me/8L9k源代码在这里: https//github.com/statiks/statiks-react-native/blob/master/sources/input /index.js

How can I do to edit this value for this input ? 如何为此输入编辑此值?

Try setting value in getInitialState: 尝试在getInitialState中设置值:

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

Then, using this.state.value in the textInput: 然后,在textInput中使用this.state.value:

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

Then, update the value state on change: 然后,在更改时更新值状态:

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

It also looks like you may be passing the incorrect value down as a prop. 看起来你可能会将不正确的值作为道具传递下去。 You need to pass this.state.data instead of value: 你需要传递this.state.data而不是value:

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

I don't have your complete code, but I tested it and it seems to be working for me this way. 我没有完整的代码,但我测试了它,它似乎以这种方式为我工作。 (you may need to set an initial state of data on the parent as an empty string or data structure) (您可能需要将父级数据的初始状态设置为空字符串或数据结构)

I think there is a bug in _onChange. 我认为_onChange中存在一个错误。 Shouldn't it be 不应该

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM