reactjs - React js reducer 逻辑条件
问题描述
使用下面的代码过滤器选项。我知道它是如何工作的过滤方法。这里我怀疑'DELETE_NOTE'的逻辑陈述。note.id 将保存 id 号 0,1,2...我怀疑 action.payload 对条件做了什么。它会保存像 id 1,2,3 这样的数字吗?请清除它。
export default function reducer(state, action) {
switch(action.type) {
case 'SET_CURRENT_NOTE':
return {
...state,
currentNote: action.payload
}
case 'DELETE_NOTE':
const deleteNotes = state.notes.filter(
note => note.id !== action.payload
)
return {
state,
notes: deleteNotes
}
default:
return state;
}
}
解决方案
在删除注释时,您将使用如下所示的有效负载调度DELETE_NOTE
操作。note Id
dispatch({ type: 'DELETE_NOTE', payload: 3});
- 这里的有效载荷只不过是要删除的笔记的 id。
在reducer函数中,过滤除已删除的note Id之外的所有状态下的note,并返回更新后的状态。
case 'DELETE_NOTE':
const deleteNotes = state.notes.filter(
note => note.id !== action.payload // here payload will be 3
)
return {
...state,
notes: deleteNotes
}
推荐阅读
- java - 即使应用程序被杀死而无需重新启动服务,如何使用 WorkManager 下载文件?
- mysql - 我想在 SQL 查询中显示总工作时间和分钟数
- ios - Swift:多个 TableView 复选标记 - 选择所有行
- dynamics-crm - 在 Dynamics 9.0 中的“交互式体验案例”表单中隐藏“相关”选项卡
- cumulocity - 使用 MQTT 进行设备集成
- php - 找不到类“Collective\Html\HtmlServiceProvider”并且其他类加密器不存在
- grafana - 如何在 grafana 中修改 from 和 to 值
- javascript - 每日、每周、每月套餐的价格选项
- javascript - 如何拆分字符串并重复字符?
- google-bigquery - 今天的日期过滤器在表格图表中显示第二天的数据