简体   繁体   English

多次调用graphql突变后保持多个加载状态

[英]Keep multiple loading states after calling graphql mutation many times

I'm using react and react apollo in my application and have a question.我在我的应用程序中使用 react 和 react apollo 并且有一个问题。 I have a page that contains list of users.我有一个包含用户列表的页面。 Every item contains "+" button.每个项目都包含“+”按钮。 When I click on item I want to display spinner until the mutation will finished.当我单击项目时,我想显示微调器,直到突变完成。

Could you suggest me better solution for keeping loading for each mutation call?你能建议我更好的解决方案来保持每个突变调用的loading吗? Now I just store it into array, but I think it can be solved better现在我只是将它存储到数组中,但我认为它可以更好地解决

You need to store the relation between one loading process and the corresponding user item.您需要存储一个loading过程和相应的user项之间的关系。

I suggest (without knowing your requirements) to have a loadings array, where you add a loading item containing the user id onClick, and remove the loading item when done.我建议(不知道您的要求)有一个loadings数组,在其中添加一个包含user id onClick 的加载loading item ,并在完成后删除loading item A loading item may also contain other info if necessary.如果需要, loading item还可以包含其他信息。

You also probably need to store the relation between the (probably asynchronous) process and the loading item (or the user item ), so you know which loading item has finished.您可能还需要存储(可能是异步的) processloading item (或user item )之间的关系,以便您知道哪个loading item已完成。

You might do that by你可以这样做

  1. receiving a process id from the process when you invoke it, and store that id inside the loading item together with the user item id ,当您调用它时从process接收process id ,并将该 id 与user item id一起存储在loading item中,
  2. or you might store the loading id as a closure when you invoke the process , and remove the corresponding loading item , eg inside a .then((xyz)=>{... removeLoading(processId)...}) .或者您可以在调用process时将loading id作为闭包存储,并删除相应的loading item ,例如在.then((xyz)=>{... removeLoading(processId)...})中。

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

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