ReactJS Questions and Answers
ReactJS Questions and Answers
ReactJS Questions and Answers
Answers PDF
1. What is ReactJS?
ReactJS is an open-source, component-based front-end JavaScript library essential for the
view layer of the application. It is supported by Facebook.
ReactJS utilizes a virtual DOM-based structure to fill in the information in HTML DOM. The
virtual DOM works quickly, possessing a way that it only modifies single DOM components
rather than refreshing the complete DOM every time.
The React application comprises several components, each answerable for outputting a
small, reusable part of HTML. Components can be embedded inside other components to
permit complex applications to work out of basic building blocks.
A component can also support an internal state. For instance, a TabList component can save
a variable compared to the directly open tab.
Reusable Components
Reusing components is the primary feature of React JS. Even Facebook has executed
React as it supports the reuse of framework components. A developer can begin with
numerous components like a checkbox, button, etc.
We develop wrapper components made out of those smaller components. Then, we write
higher-level wrapper components. It goes on like that until we have this one root component,
and that component is our app.
Virtual DOM
When we are about to build a web application with high customer connection and view
updates, similar to the new form-maker on JotForm 4.0, we have to consider the available
execution problem.
Updating DOM is generally the bottleneck concerning web execution. React attempts to
understand this issue by utilizing virtual DOM and a DOM kept in memory.
All these actions are supported together by a focal dispatcher to refresh stores. All views are
restored according to the stores. There is likewise Redux, which is an upgraded version of
flux architecture.
It has an individual store which is not needed in flux. Redux also allows a feature where
middleware can be characterized to capture dispatched actions.
SEO-Friendly
SEO is the pillar of a strong business. A superior ranking is equivalent to more commitment
from the person, which results in more income.
In addition, ReactJS generally provides a quicker speed by decreasing the time of page
load, which is crucial for SEO.
Developer Tools
The React Developer Tools have been described as Chrome and Firefox dev extensions and
permit us to review the React component order in the virtual DOM.
It also allows us to choose specific components and check and alter their current props and
state.
Performance Enhancement
ReactJS improves execution because of virtual DOM. The DOM is a cross-platform
programming API that manages with HTML, XML, or XHTML.
Some developers face the issue when updating the DOM, which hinders the application’s
performance. ReactJS solved this problem by representing virtual DOM.
The React Virtual DOM exists in memory and is a definition of the internet browser's DOM.
When we compose a React element, we do not correspond precisely to the DOM.
Instead, we are comprising virtual elements that React will pass into the DOM, enabling
smoother and quicker performance.
Create react app is a React app standard generator developed by Facebook. It supports a
development setting configured for usability with minimal setup.
Here are some of the most important things to know about “create react app”:
Virtual DOM
In React, for each DOM object, there is a corresponding "virtual DOM object." A virtual DOM
object is a definition of a DOM object and makes a virtual replica of the initial DOM.
It is a one-way data-binding; therefore, controlling the virtual DOM is faster than updating the
initial DOM since nothing gets drawn onscreen.
JSX
JSX represents JavaScript XML. It is a JavaScript syntax extension. It is an XML or HTML
like syntax utilized by ReactJS.
This syntax is handled in JavaScript calls of React framework. It broadens the ES6 so
HTML-like content can exist together with JavaScript react code. It is not necessary to utilize
JSX, but it is endorsed to use it in ReactJS.
Simplicity
ReactJS uses the JSX file, which makes the application simple to program and understand.
We learn that ReactJS is a component-based technique that establishes the code reusable
as our need. This creates it simple to use and learn.
These props cannot be sent back to the parent component. This is how one-way data
binding works. Although, the child component can interact with the parent component to
update the state via callback functions.
React Native
React Native is a custom performed for React, directly comparable to React DOM on the
web. It uses native components rather than web components like React as building blocks.
It can start with React Native, and we must know the essential React concepts, like JSX,
components, state, and props. It also provides an approach to these platform features, from
changing the React program to working on iOS and Android.
5. What are the disadvantages of ReactJS?
The disadvantages of ReactJS are as follows:
Dynamic Technology
One of the disadvantages of ReactJS is that it keeps on changing with time. Therefore, it is a
constant learning technique for the developers, who have to understand new methods of
doing things that come with uncertain circumstances.
Average Documentation
It is another disadvantage of constantly updating technologies. There is a lack of suitable
documentation because of the fast updating of React technologies.
JSX as a barrier
ReactJS utilizes JSX, which is a syntax extension that enables the combining of HTML and
JavaScript. This method is helpful, but some development community members treat JSX as
a barrier, especially the new developers. In addition, developers argue about the learning
curve complexity of JSX.
View Part
Only UI layers of the app get covered by ReactJS. Therefore, choosing a few other
technologies is necessary to get a complete collection of tooling for the project's
development.
Example
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript" src="/path/to/react.js"></script>
<script type="text/javascript" src="/path/to/react-dom.js"></script>
<script type="text/javascript">
// Use react JavaScript program here or in an individual file
</script>
</body>
</html>
To obtain the JavaScript files, go to the https://reactjs.org/docs/getting-started.html of the
official React documentation.
React also provides JSX syntax. JSX is an extension generated by Facebook that inserts
XML syntax into JavaScript. It can use JSX we require to contain the Babel library and
change <script type="text/javascript"> to <script type="text/babel"> for translating JSX to
Javascript program.
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript" src="/path/to/react.js"></script>
<script type="text/javascript" src="/path/to/react-dom.js"></script>
<script src="https://npmcdn.com/[email protected]/browser.min.js"></script>
<script type="text/babel">
// Use react JSX program here or in an individual file
</script>
</body>
</html>
We can then use React in our project precisely as if we had installed React via npm.
7. What is the difference between ReactJS and AngularJS?
This is among the most common ReactJS interview questions. We have curated a simple
tabular comparison to help you understand the differences between ReactJS vs AngularJS.
ReactJS AngularJS
Definition ReactJS is an open-source AngularJS is an
JavaScript library. It is open-source JavaScript
used to develop a user framework that can
interface for a single-pagedevelop a powerful web
application. It is application. It is an MVC
answerable only for the framework that works
view layer of the with the MVC platform,
application. where it facilitates
development by giving a
dependable solution.
Developed By Facebook Google
Data Binding ReactJS supports one-way AngularJS uses a two-way
binding. It provides data binding, which links
singular behavior for your the Document Object
application. One-way Model (DOM) values to
data-binding defines model data. It defines if a
some changes we create new value is supported in
to the model that the app for user
influence the view, but interaction with the field.
not the other way around. It will appear in the
In these methods, the update of both the view
data only flows in one and the model.
direction.
DOM
DOM represents the Document Object Model. It is also known as HTML DOM, an
abstraction of a structured code known as HTML for web developers. DOM and HTML
code are associated, as the elements of HTML are called nodes of DOM.
It describes a structure where users can create, alter, modify documents, and present
the content. Therefore while HTML is text, DOM is an in-memory definition of this
content.
Virtual DOM (VDOM)
VDOM in ReactJS is a method that syncs the virtual user interface with the real
document object model. It is not a dedicated technology but a pattern that helps in
handling the events, updating manual DOM, as well as manipulating the attributes.
Functional Components
It is the simplest method of creating the React component. These pure JavaScript
functions obtain the props object as the first argument and return the react
component.
function Hello({ message }) {
return <h1>{`Hello, ${message}`}</h1>
}
Class components
We can use ES6 class to create a component. The function component can be written
as:
class App extends React.Component {
render() {
return <h2>{`Hii, ${this.props.message}`}</h2>
}
}
State Props
The state is mutable. Props are immutable.
The state influence data about the Props enable us to pass data from one
components. component to another as an argument.
They do not allow to generate reusable They allow to make reusable components.
components.
The State is internal and managed by the Props are external and managed by
component itself. whatever renders the component.
The child component cannot access states. The child component can access props.
Example
<MyContext.Consumer>
{value => /* render something depend on the context value */}
</MyContext.Consumer>
Elements Components
An element is a plain JavaScript object A component is the core building
representing the component state, DOM structure of the React application. It is a
node, and desired properties. class or function which takes an input
and returns a React element.
It only holds data about the component It can contain state and props and access
type, its properties, and any child the React lifecycle methods.
elements inside it.
It is immutable. It is mutable.
Example: Example:
const element = React.createElement( function Button ({ onLogin }) {
'div', return React.createElement(
{id: 'login-btn'}, 'div',
{id: 'login-btn', onClick: onLogin},
'Login' 'Login'
) )
}
It can work with all the lifecycle It cannot work with any lifecycle method
methods of React. of React.
9. When to use the class component over a function
component?
If the component requires state or lifecycle methods, then use class components;
otherwise, use function components.
From React 16.8, with the inclusion of hooks, we can use state, lifecycle methods,
and other characteristics that were only feasible in a class component right in our
function component.
componentWillMount() componentDidMount()
This is called only once through the It is called only on the user end. It is
component lifecycle before the element generally performed after the initial
is rendered to the server. It is used to render when a user has received data
determine props and do any excess logic from the server.
based on them.
It is used to execute state changes It enables us to implement all kinds of
before the initial render. advanced interactions, including state
changes.
It is invoked just directly after the Putting the data loading code in this
mounting has occurred. only ensures that data is fetched from
the client's end.
Users should avoid using async The data does not store until the initial
initialization to minimize side effects or render is done. Hence, users must set an
subscriptions in this method. initial state properly to eradicate
undefined state errors.
Binding in Constructor
The methods are not constrained by default in JavaScript classes. A similar thing
relates to react event handlers represented as class methods. It can generally bind
them in the constructor.
class Component extends React.Component {
constructor(props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
// ...
}
}
If we don't like to use the bind method, then public class fields syntax can be used to
bind callbacks accurately.
handleClick = () => {
console.log('this is:', this)
}
<button onClick={this.handleClick}>
{'Click me'}
</button>
Component Creation
Default values for the component props are stated by framework the defaultProps
property of the class:
MyReactClass.defaultProps = {
name: 'Bob',
initialCount: 0
};
Replacing getInitialState()
The idiomatic way to set up the component's initial state is to set this. state in the
constructor:
constructor(props){
super(props);
this.state = {
count: this.props.initialCount
};
}
Component Removal
This method is known before a component is unmounted from the DOM. This phase
includes only one method and is given below.
componentWillUnmount()
This method is invoked directly before a component is destroyed and unmounted
permanently. It implements any necessary cleanup related function, including
invalidating timers, and event listeners, canceling network requests, or cleaning up
the DOM component. If a component instance is unmounted, we cannot mount it
again.
Example
import React, { Component } from 'react';
export default class SideMenu extends Component {
constructor(props) {
super(props);
this.state = {
...
};
this.openMenu = this.openMenu.bind(this);
this.closeMenu = this.closeMenu.bind(this);
}
componentDidMount() {
document.addEventListener("click", this.closeMenu);
}
componentWillUnmount() {
document.removeEventListener("click", this.closeMenu);
}
openMenu() {
...
}
closeMenu() {
...
}
render() {
return (
<div>
<a
href = "javascript:void(0)"
className = "closebtn"
onClick = {this.closeMenu}
>
×
</a>
<div>
Some other structure
</div>
</div>
);
}
}
Component Update
It is the next stage of the lifecycle of a react component. This stage allows managing
user interaction and support interaction with the components hierarchy. In addition,
this stage aims to ensure that the component is showing the latest version of itself.
componentWillReceiveProps(nextProps)
This is the function called on properties changes. When the component's properties
change, it will call this function with the new properties. We can access the old props
with this.props and the new props with nextProps.
With these variables, we can do some comparison operations between old and new
props or call functions because of a property change, etc.
shouldComponentUpdate(nextProps, nextState)
This is the second function known as properties changes and the first on state
changes.
By default, if another component / our component changes a property/state of our
component, it will render a new version. Therefore, in this method, this function
always returns true.
We can override this function and select more accurately if our component must
update or not.
This function is generally used for optimization.
In the method of the function returns false, the update pipeline stops directly.
componentShouldUpdate(nextProps, nextState){
return this.props.name !== nextProps.name ||
this.state.count !== nextState.count;
}
componentWillUpdate(nextProps, nextState)
This function works like componentWillMount().Here, we can't modify the
component state by invoking this.setState() method. It will not be known,
if shouldComponentUpdate() returns false.
render()
It is invoked to determine this.props and this.state and return one of the following
types: React elements, Arrays and fragments, Booleans or null, String and Number. If
shouldComponentUpdate() returns false, the code within render() will be invoked
again to assure that the component shows itself correctly.
componentDidUpdate(prevProps, prevState)
It is invoked directly after the component updating appears. In this method, we can
put some code inside this, which we need to implement once the updating occurs.
This method is not invoked for the basic render.
● HTML Course
● Javascript Course
● PHP Curse
● Power BI Course
● Tableau Course