update multiple objects in array using redux toolkit

I have a store called stories which has a userStories object stored.

Example of userStories object inside store:

       userStories:  {
          userId1: [{storyId: 1, viewedBy: [id5, id2, id9]}, {storyId: 2, viewedBy: [id1, id2, id3]}]
          userId2: [{storyId: 6, viewedBy: [id25, id42, id29]}, {storyId: 7, viewedBy: [id21, id22, id33]}]
          }

I subscribe to the stories for a particular userId in my Home component like so:

const stories = useSelector((state) => state.stories.userStories[userId])

When a user clicks a button in my component, I dispatch a method called updateStories which takes an array of story ids as well as a user id. It should update all story objects for a particular user id.

Here is the reducer:

   updateStoryViewedBy: (state, action) => {
    const { storyIds, storyCreatorId} = action.payload
    const stories = state.userStories[storyCreatorId]

    // loop over all story ids => find them in users stories => update them each individually
    storyIds.forEach((id) => {
        const s = stories?.find((s) => s?._id === id)
        s.isSeen = true
    })
},

Here I am directly mutating the state. If storyIds contains n id’s, then the store is directly mutated n times, once for each object matching the story id. As each direct mutation to a store causes a re-render to a component that subscribes to that store, this would in theory update the Home component n times as it is using useSelector to subscribe to the store. However my Home component isn’t re-rendering n times.

Why is this, and is using a loop in a reducer method to update individual objects in an array considered problematic in redux-tookit?

Could it be more beneficial to perhaps use state.userStories[userId] = state.userStories[userId].map(...) to only update the store once and avoid mutating? Thanks

1

Here I am directly mutating the state. If storyIds contains n id’s,
then the store is directly mutated n times, once for each object
matching the story id. As each direct mutation to a store causes a
re-render to a component that subscribes to that store, this would in
theory update the Home component n times as it is using useSelector to
subscribe to the store. However my Home component isn’t re-rendering n
times.

I believe the assumption of each mutation causing a re-render does not happen here. The shallow equality checking may not be seeing each change because you are directly mutating the state, which you generally shouldn’t be doing. I think this should be a better way of writing the reducer to create a new array instead of updating the current one. Not too sure about the logic here so let me know if you run into any issues with this

updateStoryViewedBy: (state, action) => {
  const { storyIds, storyCreatorId } = action.payload;
  const stories = state.userStories[storyCreatorId];

  if (stories) {
    const updatedStories = stories.map(story => {
      if (storyIds.includes(story._id)) {
        return { ...story, isSeen: true };
      }
      return story;
    });

    state.userStories[storyCreatorId] = updatedStories;
  }
}

New contributor

justnut is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật