How to set previous state on useState using Typescript
Solution 1:
You defined the state as InvoiceType | null
, so prevState
can be null
. You must add a guard before using it:
prevState => prevState ? { ...prevState, customer_name: ev.target.value } : null
The another approach is to define the state so it always has a value: useState(invoice || emptyInvoice)
. And then you can keep your code for the update.