|
28 | 28 |
|
29 | 29 | ---
|
30 | 30 |
|
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. |
32 | 32 |
|
33 | 33 | ### Table of Contents
|
34 | 34 |
|
|
515 | 515 |
|
516 | 516 | Components are the building blocks of creating User Interfaces(UI) in React. There are two possible ways to create a component.
|
517 | 517 |
|
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: |
519 | 519 |
|
520 | 520 | ```jsx harmony
|
521 | 521 | function Greeting({ message }) {
|
|
616 | 616 |
|
617 | 617 | 
|
618 | 618 |
|
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. |
620 | 620 |
|
621 | 621 | ```jsx harmony
|
622 | 622 | import React, { useState } from "react";
|
|
696 | 696 | <div>
|
697 | 697 | <p>{props.name}</p>
|
698 | 698 | <p>{props.age}</p>
|
| 699 | + <p>{props.gender}</p> |
699 | 700 | </div>
|
700 | 701 | );
|
701 | 702 | };
|
702 | 703 |
|
703 | 704 | const ParentComponent = () => {
|
704 | 705 | return (
|
705 | 706 | <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"/> |
708 | 709 | </div>
|
709 | 710 | );
|
710 | 711 | };
|
711 | 712 | ```
|
712 | 713 |
|
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. |
714 | 715 |
|
715 | 716 | ```jsx harmony
|
716 |
| - const ChildComponent = ({name, age}) => { |
| 717 | + const ChildComponent = ({name, age, gender="male"}) => { |
717 | 718 | return (
|
718 | 719 | <div>
|
719 | 720 | <p>{name}</p>
|
720 | 721 | <p>{age}</p>
|
| 722 | + <p>{gender}</p> |
721 | 723 | </div>
|
722 | 724 | );
|
723 | 725 | };
|
724 | 726 | ```
|
| 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. |
725 | 728 |
|
726 | 729 | <details><summary><b>See Class</b></summary>
|
727 | 730 | The Props accessed in Class Based Component as below
|
|
736 | 739 | <div>
|
737 | 740 | <p>{this.props.name}</p>
|
738 | 741 | <p>{this.props.age}</p>
|
| 742 | + <p>{this.props.gender}</p> |
739 | 743 | </div>
|
740 | 744 | );
|
741 | 745 | }
|
|
745 | 749 | render() {
|
746 | 750 | return (
|
747 | 751 | <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" /> |
750 | 754 | </div>
|
751 | 755 | );
|
752 | 756 | }
|
|
1339 | 1343 |
|
1340 | 1344 | 38. ### What is children prop?
|
1341 | 1345 |
|
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. |
1345 | 1347 |
|
1346 | 1348 | A simple usage of children prop looks as below,
|
1347 | 1349 |
|
| 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 | +
|
1348 | 1372 | ```jsx harmony
|
1349 | 1373 | const MyDiv = React.createClass({
|
1350 | 1374 | render: function () {
|
|
1360 | 1384 | node
|
1361 | 1385 | );
|
1362 | 1386 | ```
|
| 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`. |
1363 | 1391 |
|
1364 | 1392 | **[⬆ Back to Top](#table-of-contents)**
|
1365 | 1393 |
|
|
1499 | 1527 |
|
1500 | 1528 | 45. ### Why React uses `className` over `class` attribute?
|
1501 | 1529 |
|
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. |
1503 | 1531 |
|
1504 | 1532 | ```jsx harmony
|
1505 | 1533 | render() {
|
@@ -7281,7 +7309,7 @@ const loadUser = async () => {
|
7281 | 7309 |
|
7282 | 7310 | **[⬆ Back to Top](#table-of-contents)**
|
7283 | 7311 |
|
7284 |
| -336. ### Why does strict mode render twice in React? |
| 7312 | +338. ### Why does strict mode render twice in React? |
7285 | 7313 | 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.
|
7286 | 7314 |
|
7287 | 7315 | ```js
|
@@ -7311,6 +7339,29 @@ const loadUser = async () => {
|
7311 | 7339 |
|
7312 | 7340 | **[⬆ Back to Top](#table-of-contents)**
|
7313 | 7341 |
|
| 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 | +
|
7314 | 7365 | ## Disclaimer
|
7315 | 7366 |
|
7316 | 7367 | 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