0

`We need to load the component with dynamic import

for eg: we have hello.js component in "component/shared/hello.js"

and we want to load the "hello" component in login component which is at "component/page/login"

`*const path = `components/shared/hello`;
    const DynamicComponent = dynamic(() => import(`${path}`), {
        ssr: false,
    });

return (
        <div>
            <DynamicComponent />
     </div>
    );*`

we got the error as "failed to load the component"

enter image description here as shown below this map doesnt have requested component path so its throwing error, then how do we load component with dynamic import with relative path

enter image description here

1 Answer 1

0

Instead of using dynamic import with a variable as the module path, use require with a string literal for the path, and include the full relative path to the component file.

const path = 'components/shared/hello'; // Use a string literal for the path

const DynamicComponent = dynamic(() => require(`../../${path}`).then(mod => mod.default), {
    ssr: false,
});

return (
    <div>
        <DynamicComponent />
    </div>
);

and instead of '../../' you can use on which level you have to reach the hello component.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.