Quantcast
Channel: How to dispatch a Redux action with a timeout? - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by Irfanullah Jan for How to dispatch a Redux action with a timeout?

$
0
0

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} />;}

Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>