Search code examples
javascriptreactjssweetalert2

How to display a SweetAlert popup on Dom load using Function Component in React


I recently started using react so I apologize if this is too "noobish" question. So I am trying to have a SweetAlert popup on path(URL) change is there a way this can be done using functional component?

I already checked Callback when DOM is loaded in react.js But this works only for class components..


Solution

  • This is possible using useEffect() hook.

    If you’re familiar with React class lifecycle methods, you can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.

    Since hooks can only be called within functional components.