オブジェクトfirstOccurrence
に配列extras
が含まれています。これは最初は空の配列に設定されています。次に、AJAXリクエストを行い、配列全体が置換されずに空であることを除いて正常に動作するオブジェクトfirstOccurrence
全体を置き換えます。React:オブジェクトを更新した後、オブジェクト内の配列が置換されない
アレイを含むオブジェクト全体をどのように置き換えることができますか?
import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import axios from 'axios';
export default class Job extends Component {
state = {
firstOccurrence: {
customer: {},
job: {},
extras: []
}
};
componentWillMount() {
axios.get(
`http://api.test.dev:5000/jobs/1.json`,
{
headers: { Authorization: 'Token token=123' }
}
).then(response => {
console.log(response.data);
this.setState({
firstOccurrence: response.data
})
});
}
render() {
return (
<View>
<View>
<Text>{this.state.firstOccurrence.customer.firstName} {this.state.firstOccurrence.customer.lastName}</Text>
</View>
{this.state.firstOccurrence.extras.length > 0 &&
<View>
<Text>Extras: {this.state.firstOccurrence.extras}</Text>
</View>
}
</View>
);
}
}
JSONレスポンス:
JSON応答データを投稿できますか? – Pineda
@Pineda質問に追加しました – migu
期待される出力は何ですか?代わりに何が起こりますか? – Pineda