Am working on a todo app using react redux, its initial state is an object initialState
and has an array task
which is an empty array. When setting the values of the task
, I can't seem to find a way to get the current index of the objects to assign it to id
.
I have tried using the indexOf()
and the findIndex()
but haven't come across the best way to approach it.
I want the id to be equal to its object's index. (If object is at index 0 in the task array, that id to be equal to 0) And to also work when the array is changed or has more than one object. I appreciate the help, thanks
This is my initial state
export const initialState = {
task: []
}
This is my reducer
const manageTodoReducer = (state, action) => {
switch(action.type) {
case "ADD_TODO":
return (
{
...state,
task: [
...state.task,
{
name: "This is the name",
id: state.task.findIndex(x => x.name === state.task.name),
complete: false
}]
}
)
default:
return state
}
}