简体   繁体   English

在其他组件上添加帖子后自动响应更改 state

[英]React change state automatically after a post has been added on other component

I want to create a ticket functionality.我想创建一个票证功能。 On the main page (the parent) I have a axios get function that once the page is loaded, the useEffect is listing all the tickets components.在主页(父级)上,我有一个 axios get function,一旦页面加载,useEffect 就会列出所有票证组件。

On the second page I have the child component where I imported with props the reloadTickets={fetchTickets} method to be used every time I have added a new ticket.在第二页上,我有一个子组件,我在其中导入了道具reloadTickets={fetchTickets}方法,每次我添加新票时都会使用该方法。

The problem is that If I add a post, one time the post is not added, second time the post is added twice and third time the post is adding too many times on a single form submission.问题是,如果我添加一个帖子,第一次没有添加帖子,第二次添加了两次帖子,第三次帖子在单个表单提交上添加了太多次。 Sometime the post is not added at all, but after I try more times, the post is added twice and more.....有时候帖子根本就加不上,但我试了几次后,帖子又加了两次……

  • This is my PARENT COMPOENTN这是我的父组件

在此处输入图像描述

  • This is my Child component这是我的子组件

在此处输入图像描述

PS.附言。 Sorry for the pictures.对不起的图片。 But react code cannot be formated to look good on my page.但是无法将反应代码格式化为在我的页面上看起来不错。

Well the problem is you are not waiting for axios call to finish, since axios.post method is asynchronous.那么问题是你没有等待 axios 调用完成,因为 axios.post 方法是异步的。 You need to make your function async and await axios.post method, here is an example how you can do this.您需要使 function 异步并等待 axios.post 方法,这里是一个如何执行此操作的示例。

const createTicket = async (e) => {
    e.preventDefault();

    try {
      await axios.post(postTicketURL, {
        userID: userID,
        content: ticketArea
      }, configHeader);

      await reloadTickets();
    }
    catch (error) {
      console.log(error);
    }
  }

Update: I see that you are already using.then and.catch approach in your parent component, so you can solve this with that like this:更新:我看到你已经在你的父组件中使用了.then 和.catch 方法,所以你可以这样解决这个问题:

  const createTicket = async (e: any) => {
    e.preventDefault();

    axios.post(postTicketURL, {
      userID: userID,
      content: ticketArea
    }, configHeader).then((res: any) => {
      console.log(res);
      reloadTickets();
    }).catch((err: any) => {
      console.log(err);
    });
  }

But I would recommend async await approach, and also you should refactor your code inside TicketsComponent -> fetchTickets to match that.但我会推荐 async await 方法,而且你应该在 TicketsComponent -> fetchTickets 中重构你的代码以匹配它。 Why?为什么? Because async await is much more clear and modern approach and you are avoiding potential callback hell, for example if you now want to wait reloadTickets function to end properly.因为 async await 是一种更加清晰和现代的方法,并且您正在避免潜在的回调地狱,例如,如果您现在想要等待 reloadTickets function 正确结束。 You can find out more about this topics here: https://www.loginradius.com/blog/async/callback-vs-promises-vs-async-await/您可以在此处找到有关此主题的更多信息: https://www.loginradius.com/blog/async/callback-vs-promises-vs-async-await/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM