简体   繁体   English

Relay Modern:删除乐观更新程序中的记录

[英]Relay Modern: delete record in optimisticUpdater

In my app I have a mutation where I want to use the optimisticUpdater to update the view before the server response.在我的应用程序中,我有一个mutation ,我想在服务器响应之前使用optimisticUpdater更新器更新视图。 For that I need to remove some records from a list into the relay store, like that:为此,我需要从列表中删除一些记录到中继存储中,如下所示:

optimisticUpdater: storeProxy => {
  storeProxy.delete(recordDataID)
}

The problem is that relay don't remove the record, but it transforms the record in null .问题是中继不会删除记录,而是将记录转换为null This can be annoying because I have to filter the list every time I use it in my app.这可能很烦人,因为我每次在我的应用程序中使用它时都必须过滤列表。

Some one know how can I remove the record ?有人知道如何删除记录吗? thx谢谢

You have to remove your records from the list您必须从列表中删除您的记录

optimisticUpdater: (store) => {
    const listOfRecords = store.getRoot().getLinkedRecords('list')
    const newList = listOfRecords.filter(record => record.getDataID() !== recordDataID)
    store.getRoot().setLinkedRecords(newList, 'list')
}

In this example I assume your list is placed at the root of your graph在此示例中,我假设您的列表位于图表的根部

If you decorate your query with @connection , then you can use the ConnectionHandler to easily remove records:如果您使用@connection装饰您的查询,那么您可以使用ConnectionHandler轻松删除记录:

const query = graphql`query WidgetListQuery {
    widgets(first: 10) @connection(key: "WidgetList_widgets") {
        ...
    }
}`
optimisticUpdater(store) {
    const widgets = ConnectionHandler.getConnection(store.getRoot(), 'WidgetList_widgets');
    ConnectionHandler.deleteNode(widgets, deleted_widget.id)
}

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

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