i am trying to display data in table fornmat. Data is coming from an api. i need to display table once data recived from api . i am using class component . below i have given what is tried.
//below is my jsx
<div>
<input onChange={this.handleSearchChange} placeholder="Search" />
<div>
if (!this.state.user.length) return <div>No data</div>
<table>
<thead>
<th>Color</th>
<th>Name</th>
<th>Age</th>
</thead>
<tbody> </tbody>
</table>
</div>
</div>
//below is my function to get data from api
handleSearchChange = (e) => {
const { value } = e.target;
var self = this;
axios
.post("http://localhost:3000/user", { namr: value })
.then(function (res) {
this.setState({ user: res.data }); // Error TypeError: Cannot read property 'setState' of undefined
})
.catch(function (err) {
console.log("Error", err);
});
};
//below is my api output
[
{color: "green",name: "test",age: "22"},
{color: "red",name: "test2",age: "23"}
]