You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20-6Lines changed: 20 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1586,14 +1586,26 @@ class ParentComponent extends React.Component {
1586
1586
1587
1587
33. ### What are portals in React?
1588
1588
1589
-
A Portal is a React feature that enables rendering children into a DOM node that exists outside the parent component's DOM hierarchy, while still preserving the React component hierarchy. When using
1590
-
CSS transform in a component, its descendant elements should not use fixed positioning, otherwise the layout will blow up.
1589
+
A Portal is a React feature that enables rendering children into a DOM node that exists outside the parent component's DOM hierarchy, while still preserving the React component hierarchy. Portals help avoid CSS stacking issues—for example, elements with position: fixed may not behave as expected inside a parent withtransform. Portals solve this by rendering content (like modals or tooltips) outside such constrained DOM contexts.
1591
1590
1592
1591
```javascript
1593
1592
ReactDOM.createPortal(child, container);
1594
1593
```
1594
+
*`child`: Any valid React node (e.g., JSX, string, fragment).
1595
+
*`container`:A real DOMnode (e.g., `document.getElementById('modal-root')`).
1595
1596
1596
-
The first argument is any render-able React child, such as an element, string, or fragment. The second argument is a DOM element.
1597
+
Even though the content renders elsewhere in the DOM, it still behaves like a normal child inReact. It has access to context, state, and event handling.
1598
+
1599
+
**Example:- Modal:**
1600
+
```jsx
1601
+
function Modal({ children }) {
1602
+
return ReactDOM.createPortal(
1603
+
<div className="modal">{children}</div>,
1604
+
document.body)
1605
+
);
1606
+
}
1607
+
```
1608
+
The above code will render the modal content into the body element in the HTML, not inside the component's usual location.
1597
1609
1598
1610
**[⬆ Back to Top](#table-of-contents)**
1599
1611
@@ -5040,14 +5052,16 @@ class ParentComponent extends React.Component {
5040
5052
**[⬆ Back to Top](#table-of-contents)**
5041
5053
5042
5054
209. ### What is the typical use case of portals?
5055
+
React Portals are primarily used to render UI components such as **modals, tooltips, dropdowns, hovercards, and notifications** outside of their parent component's DOM tree. This helps avoid common CSS issues caused by parent elements, such as:
5043
5056
5044
-
React portals are very useful when a parent component has overflow: hidden or has properties that affect the stacking context (e.g. z-index, position, opacity) and you need to visually “break out” of its container.
5057
+
* `**overflow: hidden**` on parent elements clipping or hiding child elements like modals or tooltips,
5058
+
* **stacking context and** `**z-index**` **conflicts** created by parent containers that prevent child elements from appearing above other content.
5045
5059
5046
-
For example, dialogs, global message notifications, hovercards, and tooltips.
5060
+
That means, you need to visually “break out” of its container. By rendering these UI elements into a separate DOM node (often directly under `<body>`), portals ensure they appear above all other content and are not restricted by the parent’s CSS or layout constraints, resulting in correct positioning and visibility regardless of the parent’s styling.
5047
5061
5048
5062
**[⬆ Back to Top](#table-of-contents)**
5049
5063
5050
-
210. ### How do you set default value for uncontrolled component?
5064
+
210. ### How do you set default value for uncontrolled component?
5051
5065
5052
5066
In React, the value attribute on form elements will override the value in the DOM. With an uncontrolled component, you might want React to specify the initial value, but leave subsequent updates uncontrolled. To handle this case, you can specify a **defaultValue** attribute instead of **value**.
0 commit comments