我有一个对象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