I have a parent component name "CreateInvoice" and a child component name "InvoiceInnerForm". In the child component, I have a complex form.
Scenario is
For creating an invoice I have created an empty object which I am setting like
const [invoice, setInvoice] = useState<IInvoice>({ ...DeepCopy(DefaultInvoiceState)});
and it's working fine. For the update case, I am trying to set Invoice state in useEffect. In below code, I have created an object in useEffect method and trying to set state but it's not reflecting changes.
I am a beginner in react-hooks. Can anyone please guide me why it's not reflecting changes.
const CreateInvoice: React.FC<Props> = (props) => {
const [invoice, setInvoice] = useState<IInvoice>({ ...DeepCopy(DefaultInvoiceState)});
useEffect(() => {
// only set for update case
const obj: IInvoice = {
invoiceId: 1,
invoiceCreatedDate: new Date(),
salesMan: {
id: "c94eb8c8-5b5d-4e36-a55e-9d1b7b2fc70c",
name: "Shoaib Nazir",
},
customer: {
id: 1,
mobile: "03137864714",
name: "Noor Baker",
route: { id: 1, name: "Railway Road" },
},
product: { id: 0, name: "" },
quantity: 0,
discount: 0,
bouns: 0,
unitPrice: 0,
discountInPercentage: 0,
total: 0,
items: [{
bouns: 0,
discount: 0,
discountInPercentage: 0,
product: { id: 1, name: "Pepsi" },
quantity: 2,
total: 240,
unitPrice: 120,
}],
subTotal: 480,
totalDiscount: 0,
totalPayableAmount: 480,
paidAmount: 0,
unPaidAmount: 480,
};
setInvoice(obj);
}, []);
return (
<InvoiceInnerForm
invoice={invoice}
></InvoiceInnerForm>
);
};
export { CreateInvoice };