So I have two form components and one notification component in a react project. When submitting the first form, the route is redirected to the second form's route, where the notification that the action of the first form was done successfully must appear. How do I achieve that using my current components.
My AddEmployee.js form (where the action takes place)
const AddEmployee = () => {
const { id } = useParams();
let history = useHistory();
const [notify, setNotify] = useState({ isOpen: false, message: '', type: '' })
const [firstName, setFirstName] = useState();
const [lastName, setLastName] = useState();
async function handleUpdate() {
let item = {
firstName: firstName,
lastName: lastName,
}
//Update API Call Result
if (result.httpStatus === 200) {
history.push("/user/" + employeeId); // The route of the second form
setNotify({
isOpen: true,
message: 'Added Successfully',
type: 'success'
})
}
}
return (
<>
<input onChange={e => setLastName(e.target.value)} name="lastName" type="text" />
<input onChange={e => setFirstName(e.target.value)} name="firstName" type="text" />
</>
);
}
export default AddEmployee;
My SingleEmployee.js form (where the notification must appear)
const SingleEmployee = () => {
const { id } = useParams();
const [notify, setNotify] = useState({ isOpen: false, message: '', type: '' })
const [firstName, setFirstName] = useState();
const [lastName, setLastName] = useState();
async function getSingleUser() {
//API to get user Info
if (result.httpStatus === 200) {
setFirstName(result.result[0].firstName);
setLastName(result.result[0].lastName);
}
}
useEffect(() => {
getSingleUser();
}, []);
return (
<>
<label>{firstName}</label><br />
<label>{lastName}</label>
<Notification
notify={notify}
setNotify={setNotify}
/>
</>
);
}
export default SingleEmployee;
And finally, my notification.js
export default function Notification(props) {
const { notify, setNotify } = props;
const handleClose = (event, reason) => {
if (reason === 'clickaway') {
return;
}
setNotify({
...notify,
isOpen: false
})
}
return (
<Snackbar
open={notify.isOpen}
autoHideDuration={2000}
anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
onClose={handleClose}>
<Alert
severity={notify.type}
onClose={handleClose}>
{notify.message}
</Alert>
</Snackbar>
)
}
So the process is when I add a new employee, I should be redirected to the SingleEemployee page, where the notification that says created successfully should appear but only after the page is redirected. How to do that?