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

Unnecessary re-render when showing multiple modals.. #162

Open
sandlz opened this issue Jun 25, 2024 · 2 comments
Open

Unnecessary re-render when showing multiple modals.. #162

sandlz opened this issue Jun 25, 2024 · 2 comments

Comments

@sandlz
Copy link

sandlz commented Jun 25, 2024

Business description

In my App, there are a lot of modals that need to be displayed, so in some business scenarios, there may be 5 or more modals displayed cascading.

As follows:
Modal-A
-- Modal-B
------ Modal-C
---------- Modal-D

Code description

Example link

  1. Declare modal constants
export const ModalConstant = {
  modalA: "Modal_A",
  modalB: "Modal_B",
  modalC: "Modal_C",
};
  1. in index.tsx, call register function
NiceModal.register(ModalConstant.modalA, ModalA);
NiceModal.register(ModalConstant.modalB, ModalB);
NiceModal.register(ModalConstant.modalC, ModalC);
  1. in App.tsx
export default function App() {
  console.log("App render");

  const showModalA = () => {
    NiceModal.show(ModalConstant.modalA, {});
  };

  return (
    <div className="App">
      <h1>Muti Modal Re-render demo</h1>
      <Button variant="secondary" onClick={showModalA}>
        Show Modal [A]
      </Button>
    </div>
  );
}
  1. muti modal.tsx
  • Only the title is different
  • In ModalA.tsx, click will show ModalB
  • In ModalB.tsx, click will show ModalC ...
import BootstrapModal from "react-bootstrap/Modal";
import NiceModal, { useModal, bootstrapDialog } from "@ebay/nice-modal-react";
import { ModalConstant } from "../constant";
import Button from "react-bootstrap/Button";

const ModalA = NiceModal.create(() => {
  const modal = useModal();

  console.log("Modal [A] render", `id: ${modal.id}, visible: ${modal.visible}`);
  const showModalB = () => {
    NiceModal.show(ModalConstant.modalB, {});
  };

  return (
    <BootstrapModal {...bootstrapDialog(modal)}>
      <BootstrapModal.Header closeButton>
        <BootstrapModal.Title>Modal A</BootstrapModal.Title>
      </BootstrapModal.Header>

      <BootstrapModal.Body>
        <p>This is modal A.</p>
        <Button variant="secondary" onClick={showModalB}>
          Show Modal [B]
        </Button>
      </BootstrapModal.Body>

      <BootstrapModal.Footer>
        <Button variant="secondary" onClick={modal.hide}>
          Close
        </Button>
        <Button variant="primary" onClick={modal.hide}>
          Save changes
        </Button>
      </BootstrapModal.Footer>
    </BootstrapModal>
  );
});

export default ModalA;

Step

  1. Call showModalA() in App.tsx, it opens the Modal, without re-rendering the App(desired);
  2. Call showModalB() in ModalA.tsx, it opens the Modal B, causes a re-render to the Modal A(not desired).
  3. Call showModalC() in ModalB.tsx, it opens the Modal C, causes a re-render to the Modal A and Modal B(not desired).
  4. Console logs
Modal [A] render id: Modal_A, visible: false
2Modal [A] render id: Modal_A, visible: true
Modal [B] render id: Modal_B, visible: false
Modal [A] render id: Modal_A, visible: true
Modal [B] render id: Modal_B, visible: true
Modal [A] render id: Modal_A, visible: true
Modal [B] render id: Modal_B, visible: true
Modal [C] render id: Modal_C, visible: false
Modal [A] render id: Modal_A, visible: true
Modal [B] render id: Modal_B, visible: true
Modal [C] render id: Modal_C, visible: true

Questions

As you can see, opening the next Modal causes the previous modals re-render.

How can I get around this?

@sandlz
Copy link
Author

sandlz commented Jun 27, 2024

@supnate can you help me? thanks.

@jackdunncode
Copy link

Hi @sandlz,

I made the same issue a while ago (#116), it was resolved but it did not fix the unnecessary re-renders.

My solution, albeit not ideal, was to implement another library (https://github.com/raotaohub/ez-modal-react) to be used as the nested modal.

This prevented re-render on the first modal, which is great. But it's not ideal having 2 libraries that do the same thing..

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

2 participants