Skip to content

Commit a71f305

Browse files
committed
Review refs and HOC topics
1 parent 1bcc874 commit a71f305

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

README.md

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ Below is a list of ReactJS interview questions and answers.
231231
|213| [How react propTypes allow different types of propTypes for one prop?](#how-react-proptypes-allow-different-types-of-proptypes-for-one-prop)|
232232
|214| [How to import an SVG as a React component](#how-to-import-an-svg-as-a-react-component)|
233233
|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)|
234236

235237
## Core ReactJS
236238

@@ -624,7 +626,7 @@ class MyComponent extends Component {
624626
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.
625627
------------------------------
626628
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*.
628630
629631
25. ### How virtual DOM works?
630632
The Virtual DOM works in three simple steps.
@@ -636,13 +638,13 @@ The Virtual DOM works in three simple steps.
636638
![ScreenShot](images/vdom3.png)
637639
----------------------------------
638640
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.
640642
641643
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.
643645
644646
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.
646648
647649
29. ### What are controlled components?
648650
@@ -657,7 +659,8 @@ A ReactJS component that controls the input elements within the forms on subsequ
657659
```
658660
30. ### What are uncontrolled components?
659661
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,
661664
```jsx
662665
class UserProfile extends React.Component {
663666
constructor(props) {
@@ -690,19 +693,19 @@ In most cases, it is recommend using controlled components to implement forms.
690693
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.
691694
692695
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.
694697
695698
33. ### What are the different phases of ReactJS component lifecycle?
696699
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.
698701
2. **Mounting:** The react component is ready to mount in the browser DOM. This phase covers **componentWillMount** and **componentDidMount** lifecycle methods.
699702
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.
700703
4. **Unmounting:** In this last phase, the component is not needed and get unmounted from the browser DOM. This phase include **componentWillUnmount** lifecycle method.
701704
![ScreenShot](images/phases.png)
702705
---------
703706
34. ### What are the lifecycle methods of ReactJS?
704707
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.
706709
- **componentDidMount:** Executed after first rendering and here all AJAX requests, DOM or state updates, and set up eventListeners should occur.
707710
- **componentWillReceiveProps:** Executed when particular prop updates to trigger state transitions.
708711
- **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:
713716
714717
35. ### What are Higher-Order components?
715718
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.
717720
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.
718721
```jsx
719722
const EnhancedComponent = higherOrderComponent(WrappedComponent);
@@ -746,15 +749,15 @@ function HOC(WrappedComponent) {
746749
```
747750
748751
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.
750753
```jsx
751754
const {Provider, Consumer} = React.createContext(defaultValue);
752755

753756
```
754757
755758
38. ### What is children prop?
756759
757-
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.
758761
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.
759762
760763
A simple usage of children prop looks as below,
@@ -775,12 +778,12 @@ ReactDOM.render(
775778
```
776779
39. ### How to write comments in ReactJS?
777780
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
779782
780783
**Single-line comments:**
781784
```jsx
782785
<div>
783-
{/* Single-line comments */}
786+
{/* Single-line comments */} // In vanilla JavaScript, the single-line comments are represented by double slash(//)
784787
Welcome {user}, Let's play React
785788
</div>
786789
```
@@ -827,7 +830,7 @@ class MyComponent extends React.Component {
827830
}
828831
```
829832
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.
831834
832835
41. ### What is reconciliation?
833836
@@ -1496,7 +1499,7 @@ It solved the below two problems
14961499
---------------
14971500
89. ### What is the difference between constructor and getInitialState?
14981501
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**
15001503
```jsx
15011504
class MyComponent extends React.Component {
15021505
constructor(props) {
@@ -1505,7 +1508,7 @@ class MyComponent extends React.Component {
15051508
}
15061509
}
15071510
```
1508-
######Using React.createClass
1511+
**Using React.createClass**
15091512
```jsx
15101513
var MyComponent = React.createClass({
15111514
getInitialState() {
@@ -3182,7 +3185,7 @@ const App = () => (
31823185
);
31833186
```
31843187
**Note**: Don't forget the curly braces in the import!. This feature is available with react-scripts@2.0.0 and higher.
3185-
---------------------------------
3188+
31863189
215. ### Why are inline ref callbacks or functions not recommended?
31873190
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.
31883191
```jsx
@@ -3226,6 +3229,33 @@ class UserForm extends Component {
32263229
)
32273230
}
32283231
}
3229-
32303232
```
3233+
216. ### What is render hijacking in reactjs?
32313234
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+
function ppHOC(WrappedComponent) {
3244+
return class PP extends React.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**.
3253+
```jsx
3254+
function iiHOC(WrappedComponent) {
3255+
return class Enhancer extends WrappedComponent {
3256+
render() {
3257+
return super.render()
3258+
}
3259+
}
3260+
}
3261+
```

0 commit comments

Comments
 (0)