Next.js Discord

Discord Forum

event after dynamic component loaded

Unanswered
Common carp posted this in #help-forum
Open in Discord
Common carpOP
I’m using a 3rd party component that I need to load with ‘dynamic()’ and am trying to run some client side JS after the component is loaded and in the DOM. Is there a good way of tapping into that event?

5 Replies

@Common carp you can!
const DynamicComponent = dynamic(() => import('./YourComponent'), { ssr: false });
const Page = () => {
    const componentRef = useRef(null);

    useEffect(() => {
        if (componentRef.current) {
            console.log('Dynamic component has been loaded and is in the DOM');
        }
    }, [componentRef.current]);

    return (
        <div>
            <DynamicComponent ref={componentRef} />
        </div>
    );
};

export default Page;
import React, { forwardRef } from 'react';

const YourComponent = forwardRef((props, ref) => {
    // you can put third party component here
    return <div ref={ref}>This is your component</div>;
});

export default YourComponent;
Try these out and let me know if you have any trouble