Real DOM vs Virtual DOM
Real DOM Virtual DOM
1. It updates slow. 1. It updates faster.
2. Can directly update HTML. 2. Can’t directly update HTML.
3. Creates a new DOM if element updates. 3. Updates the JSX if element updates.
4. DOM manipulation is very expensive. 4. DOM manipulation is very easy.
5. Too much of memory wastage. 5. No memory wastage.
3. What are the features of React?
Major features of React are listed below:
i. It uses the virtual DOM instead of the real DOM.
ii. It uses server-side rendering.
iii. It follows uni-directional data flow or data binding.
4. List some of the major advantages of React.
Some of the major advantages of React are:
i. It increases the application’s performance
ii. It can be conveniently used on the client as well as server side
iii. Because of JSX, code’s readability increases
iv. React is easy to integrate with other frameworks like Meteor, Angular, etc
v. Using React, writing UI test cases become extremely easy
5. What are the limitations of React?
Limitations of React are listed below:
i. React is just a library, not a full-blown framework
ii. Its library is very large and takes time to understand
iii. It can be little difficult for the novice programmers to understand
iv. Coding gets complex as it uses inline templating and JSX
6. What is JSX?
JSX is a shorthand for JavaScript XML. This is a type of file used by React which
utilizes the expressiveness of JavaScript along with HTML like template syntax. This
makes the HTML file really easy to understand. This file makes applications robust and
boosts its performance. Below is an example of JSX:
1 render(){
return(
2
, 3
4 <div>
5
6 <h1> Hello World from Edureka!!</h1>
7
8 </div>
9
10 );
}
11
7. What do you understand by Virtual DOM? Explain its working.
A virtual DOM is a lightweight JavaScript object which originally is just the copy of the
real DOM. It is a node tree that lists the elements, their attributes and content as
Objects and their properties. React’s render function creates a node tree out of the
React components. It then updates this tree in response to the mutations in the data
model which is caused by various actions done by the user or by the system.
This Virtual DOM works in three simple steps.
1. Whenever any underlying data changes, the entire UI is re-rendered in Virtual
DOM representation.
2. Then the difference between the previous DOM representation and the new one
is calculated.
, 3. Once the calculations are done, the real DOM will be updated with only the
things that have actually changed.
8. Why can’t browsers read JSX?
Browsers can only read JavaScript objects but JSX in not a regular JavaScript object.
Thus to enable a browser to read JSX, first, we need to transform JSX file into a
JavaScript object using JSX transformers like Babel and then pass it to the browser.
12. Explain the purpose of render() in React.
Each React component must have a render() mandatorily. It returns a single React
element which is the representation of the native DOM component. If more than one
HTML element needs to be rendered, then they must be grouped together inside one
enclosing tag such as <form>, <group>,<div> etc. This function must be kept pure i.e.,
it must return the same result each time it is invoked.
13. How can you embed two or more components into one?
We can embed components into one in the following way:
1 class MyComponent extends React.Component{
2 render(){
, 3
return(
4
5
<div>
6
7
<h1>Hello</h1>
8
9
<Header/>
10
</div>
11
12 );
13 }
14 }
15 class Header extends React.Component{
16 render(){
return
17
18
<h1>Header Component</h1>
19
20
};
21
}
22
ReactDOM.render(
23
<MyComponent/>, document.getElementById('content')
24 );
25
14. What is Props?
Props is the shorthand for Properties in React. They are read-only components which
must be kept pure i.e. immutable. They are always passed down from the parent to the
child components throughout the application. A child component can never send a prop
back to the parent component. This help in maintaining the unidirectional data flow and
are generally used to render the dynamically generated data.
15. What is a state in React and how is it used?