I cant show the JSON response to the HTML, all I am getting is his.state.selectedData.map is not a function.
This is the payload
{
"id": 1,
"name":"john",
"age" : 22
}
This is in the constructor
this.state = {
selectedData : []
}
This is the HTTP request:
axios.post("/api/data", data)
.then(res =>{
console.log(res)
this.setState({
selectedData : res.data
})
})
And this is how I am trying to show the result
<div>
<ul>
{ this.state.selectedData.map(data => {
<li>{data.name}</li>
})}
</ul>
</div>
what I am doing wrong ?
res.datalook like the object in your first snippet? That is not an array, so trying to usemapon that object will give rise to your error. Maybe you want to writethis.setState({ selectedData: [res.data] });?[res.data]it does not work either[res.data]? Consider creating a Minimal, Complete, and Verifiable example in e.g. CodeSandbox and it will be easier to say what might be wrong.[res.data]i haveTypeError: Cannot read property 'map' of undefined