In Redux Toolkit, the prepare callback allows you to customize how the action’s payload is created before it reaches the reducer. It is used inside a reducer definition within createSlice() and returns an object containing payload, and optionally, meta and error fields.
To preprocess or format arguments before passing them as the action payload.
To include additional fields such as timestamps or IDs along with the payload.
To maintain clean reducer logic by separating payload creation from state updates.
Here, the prepare callback formats the payload with an auto-generated ID and timestamp before the action reaches the reducer, keeping the reducer focused solely on updating the state.