I'm trying to getting familiarity with Redux trying tio build a simple todo list. I'm struggling to understand why with this code I can't remove a single todo from the list. I would like to use the index instead of the usual id for each todo. This is what I have done so far:
export const saveTodo = createSlice({
name: 'manageList',
initialState,
reducers: {
addTodoToList: (state, action) => {
state.todoList.push(action.payload)
},
removeTodoToList: (state, action) => {
state.todoList.filter((todo, index) => index !== action.payload)
}
}
})
And this is inside the List component where I dispatch the reducer:
<div className='list'>
{todoList?.map((todo, index) => {
return <h1 key={index}>{todo}<span onClick={() => dispatch(removeTodoToList(index))}>x</span></h1>
})}
</div>
Why this approach doesn't work? Considered the usage of the redux toolkit, Shall I use mutability functions like splice instead? Assigning an id to the todo, would have make it a better choice to filter it? thanks