This may be a bit off-topic but I want to share it here because I simply wanted to remove Alerts from state after a given timeout i.e. auto hiding alerts/notifications.
I ended up using setTimeout()
within the <Alert />
component, so that it can then call and dispatch a REMOVE
action on given id
.
export function Alert(props: Props) { useEffect(() => { const timeoutID = setTimeout(() => { dispatchAction({ type: REMOVE, payload: { id: id, }, }); }, timeout ?? 2000); return () => clearTimeout(timeoutID); }, []); return <AlertComponent {...props} />;}