簡體   English   中英

設置狀態[react-native]后無法編輯inputText

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

發送獲取請求后,保存在狀態數據中,並顯示保存在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

如何為此輸入編輯此值?

嘗試在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 });
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM