Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 反应js还原逻辑条件_Reactjs_Filter_React Hooks_Conditional Statements - Fatal编程技术网

Reactjs 反应js还原逻辑条件

Reactjs 反应js还原逻辑条件,reactjs,filter,react-hooks,conditional-statements,Reactjs,Filter,React Hooks,Conditional Statements,使用下面的代码过滤器选项。我知道过滤器的工作原理。在这里,我怀疑“删除注释”的逻辑陈述。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 {

使用下面的代码过滤器选项。我知道过滤器的工作原理。在这里,我怀疑“删除注释”的逻辑陈述。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;
 }
}

删除便笺时,您将使用
Note Id
发送
DELETE\u Note
操作,如下所示

dispatch({type:'DELETE_NOTE',负载:3})-这里的有效负载只是要删除的注释的id

在reducer函数中,过滤状态中除已删除注释Id之外的所有注释,并返回更新状态

case 'DELETE_NOTE':
           const deleteNotes = state.notes.filter(
               note => note.id !== action.payload // here payload will be 3
           )
           return {
               ...state,
               notes: deleteNotes
           }