简体   繁体   English

react-apollo中删除突变后如何更新UI

[英]How to update the UI after a delete mutation in react-apollo

I want the UI of my app to update after running a delete mutation in a react apollo component.我希望我的应用程序的 UI 在 react apollo 组件中运行删除突变后更新。 The delete operation was successful but the UI did not update after the delete mutation.删除操作成功,但删除突变后 UI 没有更新。 Below is a copy of my code, is there anything I am not getting right?下面是我的代码的副本,有什么我不正确的地方吗?

const deleteRoom = async (roomId, client = apolloClient) => {
  const user = await getUserDetails();
  const deleteResponse = await client.mutate({
    mutation: DELETE_ROOM,
    name: 'deleteRoom',
    variables: {
      roomId,
    },
    update: (cache, { data: roomDelete }) => {
      const data = cache.readQuery({
        query: GET_ROOMS_QUERY,
        variables: {
          location: user.location,
          office: '',
          page: 1,
          perPage: 8,
        },
      });
      data.allRooms.rooms = data.allRooms.rooms.filter(
        room => room.id !== roomDelete.deleteRoom.room.id,
      );
      console.log(data, '======');
      cache.writeQuery({
        query: GET_ROOMS_QUERY,
        data,
      });
    },
  });
  return deleteResponse;
};

I expected that the UI will be updated after executing the delete mutation, however, the UI doesn't get updated unless I do a force refresh.我预计在执行删除更改后 UI 会更新,但是,除非我执行强制刷新,否则 UI 不会更新。

N:B When I console log the data, it actually removed the deleted data after filtering it out of the array. N:B 当我控制台记录数据时,它实际上在从数组中过滤掉删除的数据后删除了它。 The updated data is what I am writing back to the cache更新后的数据是我写回缓存的内容

This behaviour is described in docs:文档中描述了此行为:

The difference between cache.writeQuery and client.writeQuery is that the client version also performs a broadcast after writing to the cache. cache.writeQueryclient.writeQuery的区别在于客户端版本在写入缓存后也会进行广播。 This broadcast ensures your data is refreshed in the view layer after the client.writeQuery operation.此广播确保您的数据在client.writeQuery操作后在视图层刷新。 If you only use cache.writeQuery , the changes may not be immediately reflected in the view layer.如果您只使用cache.writeQuery ,更改可能不会立即反映在视图层中。 This behavior is sometimes useful in scenarios where you want to perform multiple cache writes without immediately updating the view layer.在您想要执行多个缓存写入而不立即更新视图层的情况下,此行为有时很有用。

You can use refetchQueries option in mutate to force refresh.您可以在mutate使用refetchQueries选项来强制刷新。

Make sure the object going into update as the "data" includes returning values and is not an empty object and thus will not update UI.确保要更新的对象,因为“数据”包括返回值并且不是空对象,因此不会更新 UI。

update: (cache, { data: roomDelete }) // log data update: (cache, { data: roomDelete }) //记录数据

So the mutations can work but no UI changes at once without update passing data forward.因此,突变可以工作,但不会立即更改 UI,而无需更新向前传递数据。

And cache.writeQuery works fine.而且 cache.writeQuery 工作正常。

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

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