[英]How to use async, await in React
I'm trying to make my code simple and I would like to use async and await for the code below. 我试图使我的代码简单,我想对下面的代码使用async和await。 however, I don't know how to imply...It'd be nice to have some example codes.
但是,我不知道该如何暗示。。。最好有一些示例代码。
deletePost = (post) => {
var post_id = post;
try {
axios.delete(`http://localhost:4000/deleteAll/comments/${post_id}`)
.then(() => {
axios.delete(`http://localhost:4000/posts/${post_id}`);
}).then(() => {
axios.get('http://localhost:4000/posts')
.then(({data}) => {
this.setState({
posts: data.result
})
})
})
}
catch(err) {
console.error(err);
}
}
. 。
// This is what i understand so far.
deletePost = async(post) => {
var post_id = post;
try {
var deleteComments = await axios.delete(`http://localhost:4000/deleteAll/comments/${post_id}`);
var deletePost = await axios.delete(`http://localhost:4000/posts/${post_id}`);
var getPost = await axios.get('http://localhost:4000/posts'); // How can I change the state here??
// return ??
}
catch {
...
}
}
This is what you want to do (you don't need to save undefined
values returned from the 2 first requests, and your code with promises isn't executed sequentially, the second promise isn't returned, so it isn't in a chain): 这就是您想要做的(您不需要保存从两个第一个请求返回的
undefined
值,并且带有promise的代码不会按顺序执行,不会返回第二个promise,因此它不在链):
deletePost = async(post) => {
let post_id = post;
try {
await axios.delete(`http://localhost:4000/deleteAll/comments/${post_id}`);
await axios.delete(`http://localhost:4000/posts/${post_id}`);
const {data} = await axios.get('http://localhost:4000/posts');
this.setState({
posts: data.result
})
}
catch {
...
}
}
Of you can write even such way at the end of the code: 您甚至可以在代码末尾以这种方式编写代码:
this.setState({
posts: (await axios.get('http://localhost:4000/posts')).data.result
})
You are pretty much there. 你几乎在那里。
Only 2 suggestions 仅2条建议
Once you have the updated posts set state like this 拥有更新的帖子后,设置状态如下
const response = await axios.get('http://localhost:4000/posts'); this.setState({ posts: response.result });
Full code 完整代码
deletePost = async(post) => {
var post_id = post;
try {
await axios.delete(`http://localhost:4000/deleteAll/comments/${post_id}`);
await axios.delete(`http://localhost:4000/posts/${post_id}`);
let response = await axios.get('http://localhost:4000/posts');
this.setState({
posts: response.result
});
}
catch {
...
}
}
you can see a lot of examples here , but to implement async/await in your function try using this code: 您可以在这里看到很多示例,但是要在函数中实现异步/等待,请尝试使用以下代码:
async deletePost(post) {
try {
const post_id = post;
if (post_id) {
// We do not assign the response to a variable because we do not need it for now.
await axios.delete(`http://localhost:4000/deleteAll/comments/${post_id}`);
await axios.delete(`http://localhost:4000/posts/${post_id}`);
// Assign the response to a variable to get the 'posts' data.
const response = await axios.get('http://localhost:4000/posts');
const { result } = response;
if (result) {
this.setState({
posts: result,
});
}
}
} catch (error) {
console.log('deletePost error:' , error);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.