Skip to content

Commit 2b1848f

Browse files
committed
Modifying react basic questions
1 parent 456e615 commit 2b1848f

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

README.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,7 @@ class ParentComponent extends React.Component {
13661366
**[⬆ Back to Top](#table-of-contents)**
13671367
13681368
26. ### What is children prop?
1369-
1370-
_Children_ is a prop that allows you to pass components as data to other components, just like any other prop you use. Component tree put between component's opening and closing tag will be passed to that component as `children` prop.
1369+
The `children` prop is a special prop in React used to pass elements between the opening and closing tags of a component. It is commonly used in layout and wrapper componnents.
13711370
13721371
A simple usage of children prop looks as below,
13731372
@@ -1389,6 +1388,9 @@ class ParentComponent extends React.Component {
13891388
);
13901389
}
13911390
```
1391+
Here, everything inside `<MyDiv>...</MyDiv>` is passed as children to the custom div component.
1392+
1393+
The children can be text, JSX elements, fragments, arrays and functions(for advance use case like render props).
13921394
13931395
<details><summary><b>See Class</b></summary>
13941396
<p>
@@ -1439,6 +1441,8 @@ class ParentComponent extends React.Component {
14391441
</div>
14401442
```
14411443
1444+
You can use `//` and `/* */` in JS logic, hooks, and functions.
1445+
14421446
**[⬆ Back to Top](#table-of-contents)**
14431447

14441448
28. ### What is reconciliation?
@@ -1478,13 +1482,46 @@ class ParentComponent extends React.Component {
14781482

14791483
30. ### Why React uses `className` over `class` attribute?
14801484

1481-
The attribute names written in JSX turned into keys of JavaScript objects and the JavaScript names cannot contain dashes or reserved words, it is recommended to use camelCase wherever applicable in JSX code. The attribute `class` is a keyword in JavaScript, and JSX is an extension of JavaScript. That's the principle reason why React uses `className` instead of `class`. Pass a string as the `className` prop.
1485+
React uses **className** instead of **class** because of a JavaScript naming conflict with the class keyword.
14821486

1483-
```jsx harmony
1484-
render() {
1485-
return <span className="menu navigation-menu">{'Menu'}</span>
1486-
}
1487-
```
1487+
1. `class` is a reserved keyword in JavaScript
1488+
In JavaScript, class is used to define ES6 classes:
1489+
1490+
```js
1491+
class Person {
1492+
constructor(name) {
1493+
this.name = name;
1494+
}
1495+
}
1496+
```
1497+
If you try to use class as a variable or property name, it will throw a syntax error. Since JSX is just JavaScript with XML-like syntax, using class directly in JSX would break the parser.
1498+
1499+
2. JSX Is JavaScript
1500+
1501+
When you write JSX like this:
1502+
```jsx
1503+
<div class="btn">Click</div>
1504+
```
1505+
It will be compiled to:
1506+
```jsx
1507+
React.createElement('div', { class: 'btn' }, 'Click');
1508+
```
1509+
But `class` is invalid in this object literal context (since it clashes with the JS keyword), hence React instead uses className.
1510+
```jsx
1511+
<div className="btn">Click</div>
1512+
```
1513+
which compiles to:
1514+
```jsx
1515+
React.createElement('div', { className: 'btn' }, 'Click');
1516+
```
1517+
React then translates `className` to` class` in the final HTML DOM.
1518+
1519+
3. Aligns with DOM APIs
1520+
In vanilla JavaScript, you interact with element classes using:
1521+
```js
1522+
element.className = 'my-class';
1523+
```
1524+
React follows this convention, staying consistent with the DOM API's property name rather than HTML’s attribute.
14881525
14891526
**[⬆ Back to Top](#table-of-contents)**
14901527
@@ -1549,7 +1586,7 @@ class ParentComponent extends React.Component {
15491586
15501587
33. ### What are portals in React?
15511588
1552-
_Portal_ is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. When using
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
15531590
CSS transform in a component, its descendant elements should not use fixed positioning, otherwise the layout will blow up.
15541591

15551592
```javascript

0 commit comments

Comments
 (0)