I've been trying for a long time to implement the function to update my transactions, but I can't type in the input. I saw several similar questions on stackoverflow, but NONE helped me, I'm using modal and I believe the problem is because I'm receiving an object and I'm trying to manipulate another. However, I am not able to solve. Can anyone tell me how to proceed and explain a little how to avoid this?
My TransactionTable:
interface Transaction {
id: string;
title: string;
amount: number;
type: string;
category: string;
createdAt: string;
}
export function TransactionsTable() {
(...)
const [editingTransaction, setEditingTransaction] = useState<Transaction>()
function onOpenUpdateTransactionModal(transaction: Transaction) {
setEditingTransaction(transaction)
setIsUpdateTransactionModalOpen(true)
}
return (
(...)
<PencilSimple
size={20}
weight="fill"
onClick={() => onOpenUpdateTransactionModal(transaction)}
/>
(...)
<UpdateTransactionModal
isOpen={isUpdateTransactionModalOpen}
onRequestClose={onCloseUpdateTransactionModal}
editingTransaction={editingTransaction}
/>
)
}
My UpdateTransactionModal:
interface UpdateTransactionModalProps {
isOpen: boolean;
editingTransaction: Transaction | undefined;
onRequestClose: () => void;
}
export function UpdateTransactionModal({ isOpen, onRequestClose, editingTransaction }: UpdateTransactionModalProps) {
(...)
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
if(event.target.name === 'title') {
setTitle(event.target.value)
}
return(
(...)
<S.Container onSubmit={handleUpdateTransaction}>
<h2>Atualizar transação</h2>
<input
type="text"
placeholder="Título"
name="title"
value={editingTransaction?.title}
onChange={handleInputChange}
/>
<input
type="number"
placeholder="Valor"
value={editingTransaction?.amount}
onChange={e => setTitle(e.target.value)}
/>
)
}
}
I omitted a good part of the code, as I assumed it wasn't part of the resolution and didn't want to prolong the question, but if you want to see the whole code, it's here:
setTitle, which doesn't modifyeditingTransaction.title, which is what you set the input value to be. You pass ineditingTransactionas a prop, if you want to do this, you need to communicate to the parent component thateditingTransactionhas changed.