[英]Cannot read properties of undefined (reading 'setState'), Laravel-React , I'm new fresher for laravel-react
enter code here
class 表擴展組件 {
constructor(props) {
super(props);
this.state = {
employee: [],
}
}
//Life Sycle Methord
componentDidMount() {
this.getEmployeeList();
}
// Get Employee List
getEmployeeList = (e) => {
axios.get('get/employee/list').then(function (response) {
console.log(response.data);
this.setState({
employee : response.data,
});
})
}
render() {
return (
<div className="container">
<div className="row justify-content-center">
<div className="col-md-8">
<div className="card">
<table className="table table-hover">
<thead>
<tr>
<th scope="col" width="100px">#</th>
<th scope="col" width="100px">Name</th>
<th scope="col" width="100px">Slary</th>
<th scope="col" width="100px">Action</th>
</tr>
</thead>
<tbody>
{this.state.employee.map(function (x, i) {
<TableRow key={i} data={x} />
})}
</tbody>
</table>
</div>
</div>
</div>
</div>
);
}
}
導出默認表;
當我嘗試此代碼時(如上所述圖像https://i.stack.imgur.com/gazFY.png ),我收到如下錯誤...
“無法讀取未定義的屬性(讀取'setState')”
你可以直接在getEmployeeList中調用this.setState..
this.setState({
employee : response.data,
});
是否有任何特殊原因,您將其分配給變量 self. 此關鍵字在普通 function 和箭頭 function 中的作用也不同。
參考..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.