缓存已更新,但在删除突变 NEXTJS SSR 后更新缓存时 UI 不会更改

时间:2021-03-24 15:49:36

标签: next.js server-side-rendering apollo-client apollo-cache-inmemory

我正在尝试使用 SSR 在 Nextjs 中发生突变后更新缓存。我可以更新缓存,但用户界面不反映缓存的变化

在使用 getServerSideProps 获取数据时无法更新 UI,因为缓存是由服务器设置的??

这是我更新缓存的代码。

onClick={async () => {
    await removeFromCart({
         variables: { productId: p!.product.id as string },
               update(cache, el) {
                  const deletedId = el.data?.removeFromCart
                    console.log(deletedId)
                      const cartItems = cache.readQuery<MyCartQuery>({
                           query: MyCartDocument,
                          })
                    const filteredProds = cartItems?.myCart.products?.filter(
                      (p) => p?.product.id !== deletedId
                  )
                cache.writeQuery({
                    query: MyCartDocument,
                        data: {
                          myCart: {
                           ...cartItems,
                           products: filteredProds,
                         },
                     },
                   })
                 },
                })
             }}

0 个答案:

没有答案
相关问题