Tengo el estado actual: const [newInvoice, setInvoice] = useState<InvoiceType | null>(invoice)
Mi tipo para InvoiceType es:
customer_email: string customer_name: string description: string due_date: string status: FilterButtonState total: number line_items: LineItemType[]Estoy tratando de modificar el estado en mi formulario para modificar el estado anterior usando un campo de entrada como el siguiente:
<input ...otherAttributes onChange={(ev: React.ChangeEvent<HTMLInputElement>): void => setInvoice((prevState) => ({ ...prevState, customer_name: ev.target.value })) /> Pero sigo recibiendo el siguiente error de tipo: Argument of type '(prevState: InvoiceType | null) => { customer_name: string; customer_email?: string | undefined; description?: string | undefined; due_date?: string | undefined; status?: FilterButtonState | undefined; total?: number | undefined; line_items?: LineItemType[] | undefined; }' is not assignable to parameter of type 'SetStateAction<InvoiceType | null>'
No estoy seguro de cómo estructurar esto para poder solucionar este problema. Gracias por la ayuda.
Ha definido el estado como InvoiceType | null , por lo que prevState puede ser null . Debe agregar un protector antes de usarlo:
prevState => prevState ? { ...prevState, customer_name: ev.target.value } : null El otro enfoque es definir el estado para que siempre tenga un valor: useState(invoice || emptyInvoice) . Y luego puede conservar su código para la actualización.