TransWikia.com

How to mutate complex object with nested array with SWR?

Stack Overflow Asked by GeraltDieSocke on January 13, 2021

TL;DR:

How can I spread a new object inside a nested array e.g. data.posts.votes and also keep all the existing state from data

Full Info:

I have data I fetch that looks like that:

enter image description here

I want to have an upvote functionality that adds a new vote object into the votes array in the screenshot above.

I really have no plan right now how to do this in the mutate function from swr:

Right now I have it like this but that won’t work:

mutate(
    `/api/subreddit/findSubreddit?name=${fullSub.name}`,
    (data) => {
      console.log(data);
      return {
        ...data,
        posts: (data.posts[postid].votes = [
          ...data.posts[postid].votes,
          { voteType: "UPVOTE" },
        ]),
      };
    },
    false
  );

How can I optimistically update this?

One Answer

I think your approach should be like this:

mutate(
  `/api/subreddit/findSubreddit?name=${fullSub.name}`,
  async (data) => {
    console.log(data);
    return {
      ...data,
      posts: data.posts.map((post) => {
        if (post.id === postid) {
          return {
            ...post,
            votes: [...post.votes, { voteType: "UPVOTE" }],
          };
        } else {
          return post;
        }
      }),
    };
  },
  false
);

Correct answer by Taghi Khavari on January 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP