0
我正在開發React/Redux CRUD form。當第一次選擇選擇選項但不是第二次時,反應/ Redux失敗
解決:由於我使用的終極版,國家應通過終極版改變了API調用後,不與this.setState。
邏輯非常基本:用戶在OwnerList中選擇所有者,然後他/她的寵物列表出現在第二個Select Form元素中。
當用戶選擇從選擇元件的所有者這種方法被稱爲:
changeOwner(value) {
this.setState({owner_id: value['value']}); // set owner
let action = ApposActionCreators.getPets(value['value'], true);
this.props.dispatch(action); // API call
this.setState({pets_options: this.props.pets_options}); // set pets
}
這是所有者選擇元件:
<Select name="owners" options={this.state.owners_options} value={this.state.owner_id} onChange={this.changeOwner.bind(this)} />
寵物元件:
<Select ref="petSelect" autofocus options={this.state.pets_options} name="selected-pet" value={this.state.pet_id} onChange={this.changePet.bind(this)} searchable={true} />
我可以成功加載所有者列表,但是當用戶從所有者列表中選擇一個元素時,第一個時間,即使動作和減速器工作正常,寵物元素仍然是而不是。 第二次及以後的時間一切正常,問題是在第一次。
我試過幾件事情等設定的setTimeout的,但沒有解決問題。
請記住'setState'是* asynchronous *。這可能是你的問題的根源。 –
API調用?那麼這個數據如何立即可用?在我看來,這可能需要幾秒鐘(如果是這種情況,可能需要升級後端)。另外,僅僅因爲你派遣了一個動作而且狀態被修改了,並不意味着你的道具被更新了。 'this.props.pets_options'只有在調用'componentWillReceiveProps'之後才能更新。 API調用不是即時的。您只能使用現有的數據進行渲染。這就是爲什麼我們有紡織師等等。 – DDS