Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to lazy load a mui dialog in nextjs when it's open? #107

Open
TheMetaverseEngineer opened this issue Mar 19, 2023 · 6 comments
Open

Comments

@TheMetaverseEngineer
Copy link

TheMetaverseEngineer commented Mar 19, 2023

Hi, I have a bunch of mui dialogs and I want to lazy load them only when they get opened. How can I do that in nextjs?

@TheMetaverseEngineer TheMetaverseEngineer changed the title How to lazy load a mui dialog when it's open? How to lazy load a mui dialog in nextjs when it's open? Mar 19, 2023
@philwolstenholme
Copy link

@HasanMothaffar
Copy link

HasanMothaffar commented May 28, 2023

Here's what I came up with:

import NiceModal from "@ebay/nice-modal-react";
import dynamic from "next/dynamic";

const LoginModal = dynamic(() => import("components/Modals/LoginModal").then((mod) => mod.LoginModal));

// Register lazy modals here
const LoginModalWrapper = () => <LoginModal id="login-modal" />;
NiceModal.register("login-modal", LoginModalWrapper);

// Use them in inner components
NiceModal.show("login-modal");

This method helped me reduce 200 KBs from the initial JS load in my app.

@supnate
Copy link
Collaborator

supnate commented May 29, 2023

Here's what I came up with:

import NiceModal from "@ebay/nice-modal-react";
import dynamic from "next/dynamic";

// Register lazy modals here
const LoginModalWrapper = () => <LoginModal id="login-modal" />;
NiceModal.register("login-modal", LoginModalWrapper);

// Use them in inner components
NiceModal.show("login-modal");

This method helped me reduce 200 KBs from the initial JS load in my app.

Just curious, why not just use NiceModal.register("login-modal", LoginModal); without wrapper.

@philwolstenholme
Copy link

Here's what I came up with:

import NiceModal from "@ebay/nice-modal-react";
import dynamic from "next/dynamic";

// Register lazy modals here
const LoginModalWrapper = () => <LoginModal id="login-modal" />;
NiceModal.register("login-modal", LoginModalWrapper);

// Use them in inner components
NiceModal.show("login-modal");

This method helped me reduce 200 KBs from the initial JS load in my app.

Just curious, why not just use NiceModal.register("login-modal", LoginModal); without wrapper.

I think that example is incomplete? I can't see what adds the lazy loading, unless I'm missing something. dynamic is imported from Next but not used?

@HasanMothaffar
Copy link

@philwolstenholme @supnate You're right, I apologize for the incomplete example. I edited the code to include the dynamic import.

@yhunko
Copy link

yhunko commented Jun 1, 2023

Here's what I came up with:

import NiceModal from "@ebay/nice-modal-react";
import dynamic from "next/dynamic";

// Register lazy modals here
const LoginModalWrapper = () => <LoginModal id="login-modal" />;
NiceModal.register("login-modal", LoginModalWrapper);

// Use them in inner components
NiceModal.show("login-modal");

This method helped me reduce 200 KBs from the initial JS load in my app.

Just curious, why not just use NiceModal.register("login-modal", LoginModal); without wrapper.

It gives a type mismatch

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants