{ id: 1, name: 'Fabio', web: 'fabiobiondi.dev' }
name
but if we used the setUser
function in this way we would lose all other properties (id
and web
):useState
function, which provides us with the previous state—the complete object containing all properties.name
property. We will use the object spread operator to merge them.name
property will be determined and updated by the content of an input field:{ id: 1, name: 'Fabio' }
.setUser
function is used to update the state.
In the onChangeName
function, setUser
is called with a function that takes the previous state and returns a new state object.(...previousState)
, and only the name
property is updated with the new value from the input.e: ChangeEvent<HTMLInputElement>
.event
property returned from the listener to the change event.
This way we are communicating to the TypeScript compiler that the property e
is an event of type ChangeEvent
and that its currentTarget
property (that is the input reference) will contain a HTMLInputElement
.