I want to add tasks to my Todo app with this action:
addTask: (state, action) => {
const newTask = {
id: uuidv4(),
text: action.payload,
completed: false,
date: action.payload,
};
state.push(newTask);
},
Which will be dispatched in this way to show both text and date:
const handleSubmit = (e) => {
e.preventDefault();
if (!inputFieldSlice || !dateSlice) return;
dispatch(addTask(`${inputFieldSlice} on: ${dateSlice}`));
dispatch(clearInputField(""));
dispatch(clearDate(""));
};
The output will be something like this: Shopping on: 2021-11-03
I wanted to know if this is the right way to do this and if I can actually pass action.payload to two different keys in an object.
action.payloaddoesn't have to be a string. You don't even need to name itpayloadoraction. You could simply pass an object as the payload with any keys you want. I chosetextanddatein this case:By storing them separately, when you render the task, you can render it like this: