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

时间:2016-02-11 22:12:26

标签: javascript ios input react-native state

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

以下是代码:

第一步我在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 }
/>

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

如何为此输入编辑此值?

2 个答案:

答案 0 :(得分:2)

尝试在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 }
/>

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

答案 1 :(得分:1)

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

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