I have a form component and a table component. On submitting the form, the table will be updated with the form inputs. But the problem is, If I navigate to another page and come back, I'm losing all the previously entered data and the table is empty. Where am I making a mistake? Thank you in advance.
const DocumentTable=()=> {
const docData = [{ id: "", name: "", code: "" }];
const [documentData, setDocumentData] = useState(docData);
const tableRows = documentData.map((info) => {
return (
<Tr>
<Td>{info.id}</Td>
<Td>{info.name}</Td>
<Td>{info.code}</Td>
</Tr>
);
});
const addRows = (data) => {
const totalDocuments = documentData.length;
data.id = totalDocuments;
const updatedDocumentData = [...documentData];
updatedDocumentData.push(data);
setDocumentData(updatedDocumentData);
};
return (
<Box>
<DocumentForm func={addRows} />
<TableContainer mt={'20'}>
<Table variant="striped" colorScheme="teal" size='lg'>
<Thead>
<Tr>
<Th>Document ID</Th>
<Th>Document Name</Th>
<Th>Document Code</Th>
</Tr>
</Thead>
<Tbody>{tableRows}</Tbody>
</Table>
</TableContainer>
{console.log(documentData)}
</Box>
);
}
export default DocumentTable;`