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
+48-18Lines changed: 48 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -231,6 +231,8 @@ Below is a list of ReactJS interview questions and answers.
231
231
|213|[How react propTypes allow different types of propTypes for one prop?](#how-react-proptypes-allow-different-types-of-proptypes-for-one-prop)|
232
232
|214|[How to import an SVG as a React component](#how-to-import-an-svg-as-a-react-component)|
233
233
|215|[Why are inline ref callbacks or functions not recommended?](#why-are-inline-ref-callbacks-or-functions-not-recommended)|
234
+
|216|[What is render hijacking in reactjs?](#what-is-render-hijacking-in-reactjs)|
235
+
|217|[What are HOC factory implementations?](#what-are-hoc-factory-implementations)|
234
236
235
237
## Core ReactJS
236
238
@@ -624,7 +626,7 @@ class MyComponent extends Component {
624
626
3. It is not composable, i.e. if a library puts a ref on the passed child, the user can't put another ref on it. Callback refs are perfectly composable.
625
627
------------------------------
626
628
24. ### What is virtual DOM?
627
-
The virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the “real” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.
629
+
The **Virtual DOM (VDOM)** is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the “real” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called *reconciliation*.
628
630
629
631
25. ### How virtual DOM works?
630
632
The Virtual DOM works in three simple steps.
@@ -636,13 +638,13 @@ The Virtual DOM works in three simple steps.
636
638

637
639
----------------------------------
638
640
26. ### What is the difference between ShadowDOM and VirtualDOM?
639
-
The Shadow DOM is a browser technology designed primarily for scoping variables and CSS in web components. The virtual DOM is a concept implemented by libraries in JavaScript on top of browser APIs.
641
+
The **Shadow DOM(SDOM)** is a browser technology designed primarily for scoping variables and CSS in web components. Whereas **Virtual DOM(VDOM)** is a concept implemented by libraries in JavaScript on top of browser APIs.
640
642
641
643
27. ### What is React Fiber?
642
-
Fiber is the new reconciliation engine or reimplementation core algorithm in React 16. Its main goal is to enable incremental rendering of the virtual DOM.The goal of React Fiber is to increase its suitability for areas like animation, layout, gestures, ability to pause, abort, or reuse work and assign priority to different types of updates; and new concurrency primitives.
644
+
**Fiber** is the new reconciliation engine or reimplementation core algorithm in React 16. Its main goal is to enable incremental rendering of the virtual DOM.The goal of React Fiber is to increase its suitability for areas like animation, layout, gestures, ability to pause, abort, or reuse work and assign priority to different types of updates; and new concurrency primitives.
643
645
644
646
28. ### What is the main goal of React Fiber?
645
-
The goal of React Fiber is to increase its suitability for areas like animation, layout, and gestures. Its headline feature is incremental rendering: the ability to split rendering work into chunks and spread it out over multiple frames.
647
+
The main goal of React Fiber is to increase its suitability for areas like animation, layout, and gestures. Its headline feature is incremental rendering: the ability to split rendering work into chunks and spread it out over multiple frames.
646
648
647
649
29. ### What are controlled components?
648
650
@@ -657,7 +659,8 @@ A ReactJS component that controls the input elements within the forms on subsequ
657
659
```
658
660
30. ### What are uncontrolled components?
659
661
The **Uncontrolled Component** are the one that stores its own state internally, and you query the DOM using a ref to find its current value when you need it. This is a bit more like traditional HTML
660
-
For example, in the below UserProfile component, the name input accessed using ref as below,
662
+
663
+
For example, in the below UserProfile component, the name input accessed using ref as below,
661
664
```jsx
662
665
classUserProfileextendsReact.Component {
663
666
constructor(props) {
@@ -690,19 +693,19 @@ In most cases, it is recommend using controlled components to implement forms.
690
693
JSX elements will be transpiled to createElement JS syntax to create React elements which are going to be used for the object representation of UI. Whereas cloneElement is used to clone an element and pass it to new props.
691
694
692
695
32. ### What is Lifting State Up in ReactJS?
693
-
When several components need to share the same changing data then it is recommended to lifting the shared state up to their closest common ancestor. For example, if two child components sharing the same data from its parent then move the state to parent instead of maintaining the local state inn both child components.
696
+
When several components need to share the same changing data then it is recommended to lifting the shared state up to their closest common ancestor. For example, if two child components sharing the same data from its parent then move the state to parent instead of maintaining the local state in both child components.
694
697
695
698
33. ### What are the different phases of ReactJS component lifecycle?
696
699
There are four different phases of React component’s lifecycle:
697
-
1. **Initialization:** In this phase react component prepares setting up the initial state and default props.
700
+
1. **Initialization:** In this phase, react component prepares setting up the initial state and default props.
698
701
2. **Mounting:** The react component is ready to mount in the browser DOM. This phase covers **componentWillMount** and **componentDidMount** lifecycle methods.
699
702
3. **Updating:** In this phase, the component get updated in two ways, sending the new props and updating the state. This phase covers **shouldComponentUpdate, componentWillUpdate and componentDidUpdate** lifecycle methods.
700
703
4. **Unmounting:** In this last phase, the component is not needed and get unmounted from the browser DOM. This phase include **componentWillUnmount** lifecycle method.
701
704

702
705
---------
703
706
34. ### What are the lifecycle methods of ReactJS?
704
707
705
-
- **componentWillMount:** Executed before rendering and is used for App level configuration in your root component.
708
+
- **componentWillMount:** Executed before rendering and is used for app level configuration in your root component.
706
709
- **componentDidMount:** Executed after first rendering and here all AJAX requests, DOM or state updates, and set up eventListeners should occur.
707
710
- **componentWillReceiveProps:** Executed when particular prop updates to trigger state transitions.
708
711
- **shouldComponentUpdate:** Determines if the component will be updated or not. By default it returns true. If you are sure that the component doesn't need to render after state or props are updated, you can return false value. It is a great place to improve performance as it allows you to prevent a rerender if component receives new prop.
@@ -713,7 +716,7 @@ There are four different phases of React component’s lifecycle:
713
716
714
717
35. ### What are Higher-Order components?
715
718
716
-
A higher-order component **(HOC)** is a function that takes a component and returns a new component. Basically, it’s a pattern that is derived from React’s compositional nature
719
+
A higher-order component **(HOC)** is a function that takes a component and returns a new component. Basically, it’s a pattern that is derived from React’s compositional nature.
717
720
We call them as **“pure’ components”** because they can accept any dynamically provided child component but they won’t modify or copy any behavior from their input components.
@@ -746,15 +749,15 @@ function HOC(WrappedComponent) {
746
749
```
747
750
748
751
37. ### What is context?
749
-
Context provides a way to pass data through the component tree without having to pass props down manually at every level. For example, authenticated user, locale preference, UI theme need to be accessed in the application by many components.
752
+
**Context** provides a way to pass data through the component tree without having to pass props down manually at every level. For example, authenticated user, locale preference, UI theme need to be accessed in the application by many components.
Children is a prop(this.prop.children) that allow you to pass components as data to other components, just like any other prop you use.
760
+
**A Children prop** is a prop(this.prop.children) that allow you to pass components as data to other components, just like any other prop you use.
758
761
There are a number of 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.
759
762
760
763
A simple usage of children prop looks as below,
@@ -775,12 +778,12 @@ ReactDOM.render(
775
778
```
776
779
39. ### How to write comments in ReactJS?
777
780
778
-
The comments in ReactJS/JSX is similar to javascript multiline comments which are wrapped with curly braces
781
+
The comments in ReactJS/JSX is similar to JavaScript with little changes. Both Single-line annd Multi-line comments are wrapped with curly braces in addition as below
779
782
780
783
**Single-line comments:**
781
784
```jsx
782
785
<div>
783
-
{/* Single-line comments */}
786
+
{/* Single-line comments */}// In vanilla JavaScript, the single-line comments are represented by double slash(//)
784
787
Welcome {user}, Let's play React
785
788
</div>
786
789
```
@@ -827,7 +830,7 @@ class MyComponent extends React.Component {
827
830
}
828
831
```
829
832
830
-
The above code snippets reveals that this.props behavior is different only with in the constructor. It would be same outside the constructor.
833
+
The above code snippets reveals that this.props behavior is different with in the constructor only. It would be same outside the constructor.
831
834
832
835
41. ### What is reconciliation?
833
836
@@ -1496,7 +1499,7 @@ It solved the below two problems
1496
1499
---------------
1497
1500
89. ### What is the difference between constructor and getInitialState?
1498
1501
You should initialize state in the constructor when using ES6 classes, whereas use getInitialState method when using React.createClass
1499
-
######Using ES6 classes
1502
+
**Using ES6 classes**
1500
1503
```jsx
1501
1504
classMyComponentextendsReact.Component {
1502
1505
constructor(props) {
@@ -1505,7 +1508,7 @@ class MyComponent extends React.Component {
1505
1508
}
1506
1509
}
1507
1510
```
1508
-
######Using React.createClass
1511
+
**Using React.createClass**
1509
1512
```jsx
1510
1513
var MyComponent =React.createClass({
1511
1514
getInitialState() {
@@ -3182,7 +3185,7 @@ const App = () => (
3182
3185
);
3183
3186
```
3184
3187
**Note**: Don't forget the curly braces in the import!. This feature is available with react-scripts@2.0.0 and higher.
3185
-
---------------------------------
3188
+
3186
3189
215. ### Why are inline ref callbacks or functions not recommended?
3187
3190
If the ref callback is defined as an inline function, it will get called twice during updates, first with null and then again with the DOM element. This is because a new instance of the function is created with each render, so React needs to clear the old ref and set up the new one.
3188
3191
```jsx
@@ -3226,6 +3229,33 @@ class UserForm extends Component {
3226
3229
)
3227
3230
}
3228
3231
}
3229
-
3230
3232
```
3233
+
216. ### What is render hijacking in reactjs?
3231
3234
3235
+
The concept of render hijacking is the ability to control what a component will output from another component. It actually means that you decorate your component by wrapping it into a Higher-Order component. By wrapping you can inject additional props or make other changes, which can cause changing logic of rendering. It does not actually enables hijacking, but by using HOC you make your component behave in different way.
3236
+
3237
+
217. ### What are HOC factory implementations?
3238
+
There are two main ways of implementing HOCs in React. 1. Props Proxy (PP) and 2. Inheritance Inversion (II). They follow different approaches for manipulating the *WrappedComponent*.
3239
+
**Props Proxy**
3240
+
In this approach, the render method of the HOC returns a React Element of the type of the WrappedComponent. We also pass through the props that the HOC receives, hence the name **Props Proxy**.
3241
+
```jsx
3242
+
3243
+
functionppHOC(WrappedComponent) {
3244
+
returnclassPPextendsReact.Component {
3245
+
render() {
3246
+
return<WrappedComponent {...this.props}/>
3247
+
}
3248
+
}
3249
+
}
3250
+
```
3251
+
**Inheritance Inversion**
3252
+
In this approach, the returned HOC class (Enhancer) extends the WrappedComponent. It is called Inheritance Inversion because instead of the WrappedComponent extending some Enhancer class, it is passively extended by the Enhancer. In this way the relationship between them seems **inverse**.
0 commit comments