Невозможно прочитать свойства undefined (чтение «setState»), Laravel-React, я новичок в laravel-react
enter code here
Таблица классов расширяет Компонент {
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://stackru.com/images/972e75e73de47ab1f8978785020d6d916c138986.png ), я получаю сообщение об ошибке, как показано ниже...
«Не удается прочитать свойства неопределенного (чтение« setState »)»
1 ответ
вы можете напрямую вызвать this.setState в getEmployeeList..
constructor(props) {
super(props);
this.state = {
employee: [],
}
this.getEmployeeList = this.getEmployeeList.bind(this);
}
async functiion getEmployeeList(e) {
const response = await axios.get('get/employee/list');
console.log(response.data);
this.setState({
employee : response.data,
});
}
Есть ли какая-то особая причина, по которой вы назначаете это переменной self. также это ключевое слово работает по-разному в обычной функции и в функции стрелки..
Ссылаться..
Как работает ключевое слово this и когда его следует использовать?