import dateDiff from 'date-diff';
import moment from 'moment';
const calcDate = (date) => {
let newDate = moment(new Date(date)).fromNow();
console.log(newDate)
return newDate;
};//end of calcDate
const removeByIndex = (state=[], index) => {
};
const addToListReducer = (state=[], action) => {
let reminders;
switch (action.type) {
case 'ADD_TO_LIST':
reminders = [...state, {task: action.task, dueDate:calcDate(action.dueDate)}]
console.log('this is the reminders in the reducer', reminders);
return reminders;
case "REMOVE_FROM_LIST":
console.log("Removing from the list", action.index)
reminders = removeByIndex(state, action.index)
return reminders;
default:
return state;
} //end of switch statement
}
export default addToListReducer;
在removeByIndex功能,我傳遞的狀態(任務的全陣列)和數組的索引號。我將如何通過使用索引刪除該數組的元素。我覺得既然是反應,我需要在其中使用過濾器?
'arr.filter((ELEM,指數)=>收益指數==行動'' – mhodges
'arr.slice(0,action.index).concat(arr.slice(action.index + 1))'也可以工作 – mhodges