My Current implementation of dispatch
I have tried to dispatch items by mapping but sometimes Redux gives an undefined error so I want to try a better way in the reducer.
const dispatch = useDispatch()
const mapCheck = [
"Math",
"Spanish",
"Clean garden",
]
mapCheck.map(c=>dispatch(createCustomList(c.val)))
What I am trying to do
I am trying to get each individual Item from an array to dispatch but I am getting this output I have a todo list in which I dispatch an array as shown below:
My Reducer
import { CUSTOM_LIST } from "../actions/shoppingList";
const initialState = {
myCustomItems: []
};
const ToDoReducer = (state = initialState, action) => {
switch (action.type) {
case CUSTOM_LIST:
const customList = { id: Date.now(),val: action.custom.custom};
const custom = state.myCustomItems.concat(customList);
const existingCustomItem = state.myCustomItems.findIndex(
item => item.val === action.custom.custom
);
if (existingCustomItem >= 0) {
return {...state, myCustomItems: state.myCustomItems}
} else {
return {...state,myCustomItems: custom};
}
default:
return state;
}
};
export default ToDoReducer;
Desired Output
How do I map each item and add them with an id as shown below in the desired output. Don't worry about the id that is just Date.now()
desired output:
const DesiredOutput = [{
"id": "qm6a67ci",
"val": "Math",
},
{
"id": "qm6a62ci",
"val": "Spanish",
},
{
"id": "rkrkmroepe",
"val": "Clean garden",
}
]