From 2c9e60e05252bc729f33f3cf7969f3b44a00eea8 Mon Sep 17 00:00:00 2001 From: Ankita Gupta Date: Mon, 20 Oct 2025 23:36:40 +0530 Subject: [PATCH] added portal pattern --- .../ReactNotes/PortalPattern.jsx | 159 ++++++++++++++++++ .../Notes/ReactPatterns/ReactSidebarPage.jsx | 8 +- 2 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 client/src/pages/Notes/ReactPatterns/ReactNotes/PortalPattern.jsx diff --git a/client/src/pages/Notes/ReactPatterns/ReactNotes/PortalPattern.jsx b/client/src/pages/Notes/ReactPatterns/ReactNotes/PortalPattern.jsx new file mode 100644 index 0000000..f875a6d --- /dev/null +++ b/client/src/pages/Notes/ReactPatterns/ReactNotes/PortalPattern.jsx @@ -0,0 +1,159 @@ +import React from "react"; +import CodeBlock from "../../components/CodeBlock"; + +const PortalPattern = () => { + const examples = [ + { + title: "Basic React Portal", + code: `import React from "react"; +import ReactDOM from "react-dom"; + +function Modal({ children }) { + return ReactDOM.createPortal( +
{children}
, + document.getElementById("modal-root") + ); +} + +function App() { + return ( +
+

App Content

+ +

This is rendered in a portal!

+
+
+ ); +} + +export default App;`, + explanation: + "React Portals allow you to render a component's children into a DOM node that exists outside the main DOM hierarchy of the parent component.", + }, + { + title: "Using Portals for Modals", + code: `function App() { + return ( +
+

Website Content

+ +
+

Modal Header

+

Modal content rendered using portal.

+
+
+
+ ); +}`, + explanation: + "Portals are especially useful for modals, tooltips, and dropdowns to escape overflow or z-index issues and render at the top-level of the DOM.", + }, + ]; + + const bestPractices = [ + "Use portals for components that need to visually break out of parent container constraints.", + "Keep portal usage limited to UI overlays like modals, tooltips, or context menus.", + "Ensure proper cleanup of event listeners or side effects in portal components.", + ]; + + const commonMistakes = [ + "Rendering everything in a portal unnecessarily.", + "Forgetting to provide a DOM element to attach the portal.", + "Not handling z-index or focus management correctly for modals.", + ]; + + return ( +
+

+ React{" "} + + Portals + +

+ +
+

+ React Portals provide a way to render children into a + DOM node outside of the parent component’s DOM hierarchy. This is + helpful for UI elements like modals, tooltips, and overlays. +

+ +
+

+ Tip: Use portals when + you need components to escape overflow, z-index, or stacking + context issues. +

+
+ + {/* Examples Section */} +
+

+ Examples +

+
+ {examples.map((ex, i) => ( +
+

+ {ex.title} +

+
+ +
+

+ {ex.explanation} +

+
+ ))} +
+
+ + {/* Best Practices */} +
+

+ Best Practices +

+
    + {bestPractices.map((practice, index) => ( +
  • + {practice} +
  • + ))} +
+
+ + {/* Common Mistakes */} +
+

+ Common Mistakes +

+
    + {commonMistakes.map((mistake, index) => ( +
  • + {mistake} +
  • + ))} +
+
+ + {/* Summary */} +
+

+ Summary +

+

+ React Portals allow components to render outside their parent + hierarchy, solving overflow and z-index issues. Use them for + modals, tooltips, and overlays for clean and predictable UI + rendering. +

+
+
+
+ ); +}; + +export default PortalPattern; diff --git a/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx b/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx index 7a0668d..9d3a7ad 100644 --- a/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx +++ b/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx @@ -34,6 +34,7 @@ import CompoundComp from "./ReactNotes/CompoundComp"; import Props from "./ReactNotes/Props"; import HOC from "./ReactNotes/HOC"; +import PortalPattern from "./ReactNotes/PortalPattern"; @@ -80,13 +81,18 @@ const topicComponents = { - "What is react" : + "What is react" : , + + + + + "Portal Pattern" : , // add all other topics here };