Being new to hooks, I was converting some old code that had multiple state properties and I came across this code (lost the link) when searching how to reduce the number of useState hooks I had originally set up. The following useReducer hook code is working as expected. I think I understand that the state value is updated via setState (dispatch), what I am trying to get my head around is how the 2nd argument of the reducer actually copies the values between state and newState using the spread syntax. All the examples I've looked at use a switch statement and various actions that return the new state. Is React doing something in the background to make this copy happen? If anyone can explain how ...newState works as the action of a reducer it would be appreciated:
const reducer = (state, newState) => ({ ...state, ...newState });
const [state, setState] = useReducer(reducer, initialState);