componentwillmount関数で、反応コンポーネントの状態をapi呼び出しから更新しようとすると、期待どおりに機能しません。値が設定されていません。状態がcomponentWillMountで更新されていません
export default class ListOfProducts extends Component {
componentWillMount() {
console.log('component currently mounting');
fetchOrder().then(data => {
console.log('order api call has been finished.', data);
this.setState(data, function() {
//this should print new data but i am still getting old data
console.log('current this.state.', this.state.orders)
})
})
}
constructor(props) {
super(props);
this.state = {
"orders": {
"order_items": []
}
};
}
render() {
let productList = [];
let productUnit = (
<View>
{this.state.orders.order_items.map(function(order,i){
return <ProductListItem
delivered={true}
productSKU={3}/>
})}
</View>
);
return productUnit;
}
}
私はよく分からないが、私はあなたがコンポーネントのでcomponentWillMountに直接状態を変更することができると思う「React.Componentは{拡張」まだレンダリングされていませんsetStateを呼び出すか、setStateを呼び出すことは意味がありませんcomponentDidMount – Barry127
this.state = data is not working –
@RakeshYadavあなたは手動で値を 'this.state'に設定するべきではありません'setState()'を呼び出すときにオーバーライドされます。 –