【问题标题】:React js reducer logical conditionReact js reducer 逻辑条件
【发布时间】:2021-02-20 16:14:18
【问题描述】:

使用以下代码过滤器选项。我知道它是如何工作的过滤方法。这里我怀疑'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;
 }
}

【问题讨论】:

    标签: reactjs filter react-hooks conditional-statements


    【解决方案1】:

    在删除注释时,您将发送带有note IdDELETE_NOTE 操作,如下所示。

    dispatch({ type: 'DELETE_NOTE', payload: 3}); - 这里的payload就是要删除的note的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
               }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2021-07-01
      相关资源
      最近更新 更多