Я пытаюсь так: -
state = {
profiles: [],
data: []
}
async componentDidMount() {
try {
// this commented code works. But I want to use axios api.
// const response = await fetch(`http://localhost:8080/all/profile`);
// const json = await response.json();
// this.setState({ data: json });
// const json = await response.json();
let response = await CurdApi.getAllProfiles(); // response always undefined.
this.setState({ data: response });
} catch (error) {
console.log(error);
}
}
Мой класс CurdApi здесь: -
export default class CurdApi {
static async getAllProfiles() {
await axios({
url: 'http://localhost:8080/all/profile',
method: 'GET',
responseType: 'json',
})
.then((response) => {
return response.data;
})
.catch((error) =>{
return error.data;
});
}
}
Я новичок в ReactJs, а также в JS. Я не понимаю, как правильно использовать этот async / await. Я должен представить эти данные, когда я получаю эти данные.