I am new to React. My application uses Laravel and React.
When trying to display data received from an api call, I am getting the following error:
TypeError: this.state.userlist.map is not a function
The api call was successful and I am able to view the data from the response in the console (See screenshot below).
View.js
import React from 'react';
import axios from 'axios';
export class View extends React.Component{
constructor(props){
super(props);
this.state = {
userlist: []
};
this.usersList = this.usersList.bind(this);
}
componentDidMount(){
axios.get('http://127.0.0.1:8000/api/test')
.then(res => {
console.log(res.data);
this.setState({ userlist: res.data });
})
.catch(error => console.log(error));
}
usersList(){
this.state.userlist.map( (val,i) => {
return(
<tr>
<td>{val.fname}</td>
<td>{val.lname}</td>
<td>{val.email}</td>
<td>{val.phone}</td>
</tr>
)
})
}
render(){
return(
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
{ this.usersList() }
</tbody>
</table>
)
}
}
Screenshot of error and response in the console

mapis not a function - which suggests something is wrong withthis.state.userlist. Why are you giving up there? What's wrong? Why do you understand or not understand about this situation? Use your debugger or add some log statements or something to do some basic debugging.