This is fetchData
function used in my ReactJS class. It makes a call to API which returns an integer.
fetchData = () => {
const url = "http://localhost:8000?"+
'mytime='+this.state.mytime+
'&mytype='+this.state.mytype;
fetch(url, {
method: "GET",
dataType: "JSON",
headers: {
"Content-Type": "application/json; charset=utf-8",
}
})
.then((resp) => {
return resp.json()
})
.then((data) => {
this.setState({
chartData: [...this.state.chartData, {field1: this.state.field1, field2: data.field2}]
})
})
.catch((error) => {
console.log(error, "catch the hoop")
})
};
I don't know how to assign this result ( data.field2
) to this.state.field2 = data.field2
. How can I do it inside this code part:
.then((data) => {
this.setState({
chartData: [...this.state.chartData, {field1: this.state.field1, field2: data.field2}]
})
})
state = {
chartData: ['A', 'B',
{
field1: 'C',
field2: ''
}
]
}
.then((data) => {
const {chartData} = this.state;
console.log(chartData[2].field2=data)
})
I try to set with this condition
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.