Я думаю, что проблема заключается в привязке this
к loadData
методу.
Вы можете связать это двумя способами.
- Bind
this
в конструкторе,
constructor(props){
super(props)
this.state = {
search: '',
books: '',
}
this.loadData = this.loadData.bind(this) //Bind this here
}
Или вы можете просто использовать функцию стрелки,
loadData = () => { //Arrow function auto binds `this`
axios.get('/books/list')
.then(response => {
this.setState({
books: response.data.books
});
console.dir(response.data.books);
})
.catch(error => {
this.setState({error: true});
});
}