Skip to content

Commit b8b12ee

Browse files
committed
Improve portals questions
1 parent 2b1848f commit b8b12ee

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,14 +1586,26 @@ class ParentComponent extends React.Component {
15861586
15871587
33. ### What are portals in React?
15881588
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 with transform. Portals solve this by rendering content (like modals or tooltips) outside such constrained DOM contexts.
15911590

15921591
```javascript
15931592
ReactDOM.createPortal(child, container);
15941593
```
1594+
* `child`: Any valid React node (e.g., JSX, string, fragment).
1595+
* `container`: A real DOM node (e.g., `document.getElementById('modal-root')`).
15951596

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 in React. 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.
15971609
15981610
**[⬆ Back to Top](#table-of-contents)**
15991611
@@ -5040,14 +5052,16 @@ class ParentComponent extends React.Component {
50405052
**[⬆ Back to Top](#table-of-contents)**
50415053
50425054
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:
50435056
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.
50455059
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.
50475061
50485062
**[⬆ Back to Top](#table-of-contents)**
50495063
5050-
210. ### How do you set default value for uncontrolled component?
5064+
210. ### How do you set default value for uncontrolled component?
50515065
50525066
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**.
50535067

0 commit comments

Comments
 (0)