Skip to content

Commit b646bd6

Browse files
committed
Refactor existing questions and new questions about JSX
1 parent f4025d0 commit b646bd6

File tree

1 file changed

+65
-14
lines changed

1 file changed

+65
-14
lines changed

README.md

+65-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
---
3030

31-
**Note:** This repository is specific to ReactJS. Please check [Javascript Interview questions](https://github.com/sudheerj/javascript-interview-questions) for core javascript questions.
31+
**Note:** This repository is specific to ReactJS. Please check [Javascript Interview questions](https://github.com/sudheerj/javascript-interview-questions) for core javascript questions and [DataStructures and Algorithms](https://github.com/sudheerj/datastructures-algorithms) for DSA related questions or problems.
3232

3333
### Table of Contents
3434

@@ -515,7 +515,7 @@
515515
516516
Components are the building blocks of creating User Interfaces(UI) in React. There are two possible ways to create a component.
517517
518-
1. **Function Components:** This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as the first parameter and return React elements to render the output:
518+
1. **Function Components:** This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as the one and only one parameter and return React elements to render the output:
519519
520520
```jsx harmony
521521
function Greeting({ message }) {
@@ -616,7 +616,7 @@
616616
617617
![state](images/state.jpg)
618618
619-
Let's take an example of **User** component with message state. Here, **useState** hook has been used to add state to the User component and it returns an array with current state and function to update it.
619+
Let's take an example of **User** component with `message` state. Here, **useState** hook has been used to add state to the User component and it returns an array with current state and function to update it.
620620
621621
```jsx harmony
622622
import React, { useState } from "react";
@@ -696,32 +696,35 @@
696696
<div>
697697
<p>{props.name}</p>
698698
<p>{props.age}</p>
699+
<p>{props.gender}</p>
699700
</div>
700701
);
701702
};
702703

703704
const ParentComponent = () => {
704705
return (
705706
<div>
706-
<ChildComponent name="John" age="30" />
707-
<ChildComponent name="Mary" age="25" />
707+
<ChildComponent name="John" age="30" gender="male" />
708+
<ChildComponent name="Mary" age="25" geneder="female"/>
708709
</div>
709710
);
710711
};
711712
```
712713
713-
The properties from props object can be accessed directly using destructing feature from ES6 (ECMAScript 2015). The above child component can be simplified like below.
714+
The properties from props object can be accessed directly using destructing feature from ES6 (ECMAScript 2015). It is also possible to fallback to default value when the prop value is not specified. The above child component can be simplified like below.
714715
715716
```jsx harmony
716-
const ChildComponent = ({name, age}) => {
717+
const ChildComponent = ({name, age, gender="male"}) => {
717718
return (
718719
<div>
719720
<p>{name}</p>
720721
<p>{age}</p>
722+
<p>{gender}</p>
721723
</div>
722724
);
723725
};
724726
```
727+
**Note:** The default value won't be used if you pass `null` or `0` value. i.e, default value is only used if the prop value is missed or `undefined` value has been passed.
725728
726729
<details><summary><b>See Class</b></summary>
727730
The Props accessed in Class Based Component as below
@@ -736,6 +739,7 @@
736739
<div>
737740
<p>{this.props.name}</p>
738741
<p>{this.props.age}</p>
742+
<p>{this.props.gender}</p>
739743
</div>
740744
);
741745
}
@@ -745,8 +749,8 @@
745749
render() {
746750
return (
747751
<div>
748-
<ChildComponent name="John" age="30" />
749-
<ChildComponent name="Mary" age="25" />
752+
<ChildComponent name="John" age="30" gender="male" />
753+
<ChildComponent name="Mary" age="25" gender="female" />
750754
</div>
751755
);
752756
}
@@ -1339,12 +1343,32 @@
13391343
13401344
38. ### What is children prop?
13411345
1342-
_Children_ is a prop (`this.props.children`) 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.
1343-
1344-
There are several methods available in the React API to work with this prop. These include `React.Children.map`, `React.Children.forEach`, `React.Children.count`, `React.Children.only`, `React.Children.toArray`.
1346+
_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.
13451347
13461348
A simple usage of children prop looks as below,
13471349
1350+
```jsx harmony
1351+
function MyDiv({ children }){
1352+
return (
1353+
<div>
1354+
{children}
1355+
</div>;
1356+
);
1357+
};
1358+
1359+
export default function Greeting() {
1360+
return (
1361+
<MyDiv>
1362+
<span>{"Hello"}</span>
1363+
<span>{"World"}</span>
1364+
</MyDiv>
1365+
);
1366+
}
1367+
```
1368+
1369+
<details><summary><b>See Class</b></summary>
1370+
<p>
1371+
13481372
```jsx harmony
13491373
const MyDiv = React.createClass({
13501374
render: function () {
@@ -1360,6 +1384,10 @@
13601384
node
13611385
);
13621386
```
1387+
</p>
1388+
</details>
1389+
1390+
**Note:** There are several methods available in the legacy React API to work with this prop. These include `React.Children.map`, `React.Children.forEach`, `React.Children.count`, `React.Children.only`, `React.Children.toArray`.
13631391
13641392
**[⬆ Back to Top](#table-of-contents)**
13651393
@@ -1499,7 +1527,7 @@
14991527
15001528
45. ### Why React uses `className` over `class` attribute?
15011529
1502-
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.
1530+
The attribute names written in JSX turned into keys of JavaScript objects and the JavaScript names cannot contain dashes or reversed words, it is recommended to use camelCase whereever 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.
15031531
15041532
```jsx harmony
15051533
render() {
@@ -7281,7 +7309,7 @@ const loadUser = async () => {
72817309
72827310
**[⬆ Back to Top](#table-of-contents)**
72837311
7284-
336. ### Why does strict mode render twice in React?
7312+
338. ### Why does strict mode render twice in React?
72857313
StrictMode renders components twice in development mode(not production) in order to detect any problems with your code and warn you about those problems. This is used to detect accidental side effects in the render phase. If you used `create-react-app` development tool then it automatically enables StrictMode by default.
72867314
72877315
```js
@@ -7311,6 +7339,29 @@ const loadUser = async () => {
73117339
73127340
**[⬆ Back to Top](#table-of-contents)**
73137341
7342+
339. ### What are the rules of JSX?
7343+
The below 3 rules needs to be followed while using JSX in a react application.
7344+
7345+
1. **Return a single root element**:
7346+
If you are returning multiple elements from a component, wrap them in a single parent element. Otherwise you will receive the below error in your browser console.
7347+
7348+
```html Adjacent JSX elements must be wrapped in an enclosing tag.```
7349+
7350+
2. **All the tags needs to be closed:**
7351+
Unlike HTML, all tags needs to closed explicitly with in JSX. This rule applies for self-closing tags(like hr, br and img tags) as well.
7352+
3. **Use camelCase naming:**
7353+
It is suggested to use camelCase naming for attributes in JSX. For example, the common attributes of HTML elements such as `class`, `tabindex` will be used as `className` and `tabIndex`.
7354+
**Note:** There is an exception for aria-* and data-* attributes which should be lower cased all the time.
7355+
7356+
**[⬆ Back to Top](#table-of-contents)**
7357+
7358+
340. ### What is the reason behind multiple JSX tags to be wrapped?
7359+
7360+
Behind the scenes, JSX is transformed into plain javascript objects. It is not possible to return two or more objects from a function without wrapping into an array. This is the reason you can't simply return two or more JSX tags from a function without
7361+
wrapping them into a single parent tag or a Fragement.
7362+
7363+
**[⬆ Back to Top](#table-of-contents)**
7364+
73147365
## Disclaimer
73157366
73167367
The questions provided in this repository are the summary of frequently asked questions across numerous companies. We cannot guarantee that these questions will actually be asked during your interview process, nor should you focus on memorizing all of them. The primary purpose is for you to get a sense of what some companies might ask — do not get discouraged if you don't know the answer to all of them ⁠— that is ok!

0 commit comments

Comments
 (0)