我有一個對象firstOccurrence
,它包含一個數組extras
,我最初設置爲一個空數組。然後我做一個AJAX請求,並替換整個對象firstOccurrence
,它可以正常工作,但數組不會被替換並保留爲空。反應:更新對象後,對象內的數組不會被替換
我該如何更換包含該數組的整個對象?
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