javascript - 具有不可变的 Redux 减速器
问题描述
我想将一个帖子添加到减速器中的一组帖子中。通常我会这样做:
CREATE_NEW__POST_SUCCESS: {
return {
...state,
fetchingPosts: false,
error: null,
posts: [...state.posts, ...action.payload],
};
但是我当前的项目要求我使用 Immutable.js 和 Immutable 状态设置为 .set() 或 .merge() 或 .update()
case CREATE_NEW_POST_SUCCESS: {
return state
.set('fetchingPosts', false)
.set('posts', action.payload)
.set('postsError', null);
这会用一个帖子对象覆盖整个帖子数组。我尝试了很多东西,比如
.set('posts' ,[...state.posts, ...action.payload])
但没有快乐
解决方案
您可以使用updateIn
类似于此处的答案。
case CREATE_NEW_POST_SUCCESS: {
return state
.set('fetchingPosts', false)
.updateIn(['posts'], posts => posts.push(action.payload))
.set('postsError', null);
推荐阅读
- json - 如何在 Golang 的顶级结构中检索嵌套的 json 值?
- javascript - document.getElementsByTagName('ul')[0]["__proto__"] instanceof HTMLUListElement 为 false
- python - VSCode 无法切换 matplotlib 后端:ImportError: Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework
- matplotlib - 在每根柱子上显示柱子的值,位置不对
- python - 更快的 cosmos 数据库查询
- node.js - Mongoose 未连接到 MongoDB
- visual-studio-code - VsCode remote-ssh 在新计算机上不起作用
- python-3.x - 不使用排序库的稳定排序 - Python
- install4j - 如何隐藏或加密包含在 Install4J 中的 jar 内容
- sql - 我们如何在 Cosmos DB 中使用子查询来实现左连接?