I'm following the Redux tutorial from redux site, and in Part 8, when writing a reducer to handle todos they do the following:
const todosSlice = createSlice({
name: 'todos',
initialState,
reducers: {
//...
todoColorSelected: {
reducer(state, action) {
const { color, todoId } = action.payload
state.entities[todoId].color = color
},
prepare(todoId, color) {
return {
payload: { todoId, color }
}
}
},
//...
}
})
They make use of the prepare function; however, I wrote that code without knowing the existence of the prepare function as follows (only using the reducer) and it works as well.
const todosSlice = createSlice({
name: 'todos',
initialState,
reducers: {
//...
todoColorSelected: {
const { color, todoId } = action.payload
state.entities[todoId].color = color
},
//...
}
})
So why do we need in this case the prepare function? or is it just a simple example where it makes no difference?