Interview Question React Js

Pramod Ray
9 min readMar 6, 2021
  1. What is React js

React is a JavaScript library for building user interfaces.

React is used to build single-page applications.

React allows us to create reusable UI components.

Advatnage of React js

a). It facilitates the overall process of writing components

b). It boosts productivity and facilitates further maintenance.

c). It ensures faster rendering.

d). It guarantees a stable code.

e). It is SEO friendly.

f). It comes with a helpful developer toolset.

g). It is focused and easy-to-learn.

h). It is backed by a strong community.

3. Disadvantage of react js.

1. The high pace of development

The high pace of development has an advantage and disadvantage both. In case of disadvantage, since the environment continually changes so fast, some of the developers not feeling comfortable to relearn the new ways of doing things regularly. It may be hard for them to adopt all these changes with all the continuous updates. They need to be always updated with their skills and learn new ways of doing things.

2. Poor Documentation

It is another con which are common for constantly updating technologies. React technologies updating and accelerating so fast that there is no time to make proper documentation. To overcome this, developers write instructions on their own with the evolving of new releases and tools in their current projects.

3. View Part

ReactJS Covers only the UI Layers of the app and nothing else. So you still need to choose some other technologies to get a complete tooling set for development in the project.

4. JSX as a barrier

ReactJS uses JSX. It’s a syntax extension that allows HTML with JavaScript mixed together. This approach has its own benefits, but some members of the development community consider JSX as a barrier, especially for new developers. Developers complain about its complexity in the learning curve.

5. What is virtual DOM.

The virtual DOM (VDOM) is a programming concept where an ideal, or “virtual”, representation of a UI is kept in memory and synced with the “real” DOM by a library such as ReactDOM. This process is called reconciliation.

A virtual DOM is a lightweight JavaScript object which originally is just a 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.

Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation.

Why can’t browsers read JSX?

Browsers can only read JavaScript objects but JSX is not a regular JavaScript object. Thus to enable a browser to read JSX, first, we need to transform the JSX file into a JavaScript object using JSX transformers like Babel and then pass it to the browser.

What is Component?

Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML via a render() function.

What is Props?

Props are the shorthand for Properties in React. They are read-only components that 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.

What is a state in React and how is it used?

States are the heart of React components. States are the source of data and must be kept as simple as possible. Basically, states are the objects which determine components rendering and behaviour. They are mutable unlike the props and create dynamic and interactive components. They are accessed via this. state().

What is the arrow function in React? How is it used?

Arrow functions are more of a brief syntax for writing the function expression. They are also called ‘fat arrow‘ (=>) the functions. These functions allow to bind the context of the components properly since in ES6 auto binding is not available by default. Arrow functions are most useful while working with higher-order functions.

Differentiate between stateful and stateless components.

Stateful vs Stateless

Stateful Component:

  1. Stores info about component’s state change in memory
  2. Have the authority to change the state
  3. Contains the knowledge of past, current and possible future changes in state
  4. Stateless components notify them about the requirement of the state change, then they send down the props to them.

Stateless Component

  1. Calculates the internal state of the components
  2. 2. Do not have the authority to change the state
  3. Contains no knowledge of past, current and possible future state changes
  4. They receive the props from the Stateful components and treat them as callback functions.

What are the different phases of React component’s lifecycle?

There are three different phases of React component’s lifecycle:

  1. Initial Rendering Phase: This is the phase when the component is about to start its life journey and make its way to the DOM.
  2. Updating Phase: Once the component gets added to the DOM, it can potentially update and re-render only when a prop or state change occurs. That happens only in this phase.
  3. Unmounting Phase: This is the final phase of a component’s life cycle in which the component is destroyed and removed from the DOM.

Explain the lifecycle methods of React components in detail.

Some of the most important lifecycle methods are:

  1. componentWillMount()Executed just before rendering takes place both on the client as well as server-side.
  2. componentDidMount()Executed on the client-side only after the first render.
  3. componentWillReceiveProps() — Invoked as soon as the props are received from the parent class and before another render is called.
  4. shouldComponentUpdate()Returns true or false value based on certain conditions. If you want your component to update, return true else return false. By default, it returns false.
  5. componentWillUpdate() — Called just before rendering takes place in the DOM.
  6. componentDidUpdate()Called immediately after rendering takes place.
  7. componentWillUnmount() — Called after the component is unmounted from the DOM. It is used to clear up the memory spaces.

What are synthetic events in React?

Synthetic events are the objects which act as a cross-browser wrapper around the browser’s native event. They combine the behaviour of different browsers into one API. This is done to make sure that the events show consistent properties across different browsers.

What do you understand by refs in React?

Refs are the shorthand for References in React. It is an attribute that helps to store a reference to a particular React element or component, which will be returned by the components render configuration function. It is used to return references to a particular element or component returned by render(). They come in handy when we need DOM measurements or to add methods to the components.

class ReferenceDemo extends React.Component{display() {const name = this.inputDemo.value;document.getElementById('disp').innerHTML = name;}render() {return(<div>Name: <input type="text" ref={input => this.inputDemo = input} /><button name="Click" onClick={this.display}>Click</button><h2>Hello <span id="disp"></span> !!!</h2></div>);}}

List some of the cases when you should use Refs.

Following are the cases when refs should be used:

  • When you need to manage focus, select text or media playback
  • To trigger imperative animations
  • Integrate with third-party DOM libraries

What are Higher-Order Components(HOC)?

Higher-Order Component is an advanced way of reusing the component logic. Basically, it’s a pattern that is derived from React’s compositional nature. HOC are custom components that wrap another component within it. They can accept any dynamically provided child component but they won’t modify or copy any behaviour from their input components. You can say that HOC is ‘pure’ components.

What can you do with HOC?

HOC can be used for many tasks like:

  • Code reuse, logic and bootstrap abstraction
  • Render High jacking
  • State abstraction and manipulation
  • Props manipulation

What are Pure Components?

Pure components are the simplest and fastest components that can be written. They can replace any component which only has a render(). These components enhance the simplicity of the code and the performance of the application.

What is the significance of keys in React?

Keys are used for identifying unique Virtual DOM Elements with their corresponding data driving the UI. They help React to optimize the rendering by recycling all the existing elements in the DOM. These keys must be a unique number or string, using which React just reorders the elements instead of re-rendering them. This leads to an increase in the application’s performance.

What is Redux?

Redux is one of the most trending libraries for front-end development in today’s marketplace. It is a predictable state container for JavaScript applications and is used for the entire applications state management. Applications developed with Redux are easy to test and can run in different environments showing consistent behavior.

What are the three principles that Redux follows?

  1. Single source of truth: The state of the entire application is stored in an object/ state tree within a single store. The single state tree makes it easier to keep track of changes over time and debug or inspect the application.
  2. State is read-only: The only way to change the state is to trigger an action. An action is a plain JS object describing the change. Just like state is the minimal representation of data, the action is the minimal representation of the change to that data.
  3. Changes are made with pure functions: In order to specify how the state tree is transformed by actions, you need pure functions. Pure functions are those whose return value depends solely on the values of their arguments.

What do you understand by “Single source of truth”?

Redux uses ‘Store’ for storing the application’s entire state at one place. So all the component’s state are stored in the Store and they receive updates from the Store itself. The single state tree makes it easier to keep track of changes over time and debug or inspect the application.

List down the components of Redux.

Redux is composed of the following components:

  1. Action — It’s an object that describes what happened.
  2. Reducer — It is a place to determine how the state will change.
  3. Store — State/ Object tree of the entire application is saved in the Store.
  4. View — Simply displays the data provided by the Store.

In case you are facing any challenges with these React interview questions, please comment on your problems in the section below.

How are Actions defined in Redux?

Actions in React must have a type property that indicates the type of ACTION being performed. They must be defined as a String constant and you can add more properties to it as well. In Redux, actions are created using the functions called Action Creators. Below is an example of Action and Action Creator:

function addTodo(text) {return {type: ADD_TODO,text}}

Explain the role of Reducer.

Reducers are pure functions which specify how the application’s state changes in response to an ACTION. Reducers work by taking in the previous state and action, and then it returns a new state. It determines what sort of update needs to be done based on the type of the action, and then returns new values. It returns the previous state as it is, if no work needs to be done.

What is the significance of Store in Redux?

A store is a JavaScript object which can hold the application’s state and provide a few helper methods to access the state, dispatch actions and register listeners. The entire state/ object tree of an application is saved in a single store. As a result of this, Redux is very simple and predictable. We can pass middleware to the store to handle the processing of data as well as to keep a log of various actions that change the state of stores. All the actions return a new state via reducers.

What are the advantages of Redux?

Advantages of Redux are listed below:

  • Predictability of outcome — Since there is always one source of truth, i.e. the store, there is no confusion about how to sync the current state with actions and other parts of the application.
  • Maintainability — The code becomes easier to maintain with a predictable outcome and strict structure.
  • Server-side rendering — You just need to pass the store created on the server, to the client side. This is very useful for initial render and provides a better user experience as it optimizes the application performance.
  • Developer tools — From actions to state changes, developers can track everything going on in the application in real time.
  • Community and ecosystem — Redux has a huge community behind it which makes it even more captivating to use. A large community of talented individuals contribute to the betterment of the library and develop various applications with it.
  • Ease of testing — Redux’s code is mostly functions which are small, pure and isolated. This makes the code testable and independent.
  • Organization — Redux is precise about how code should be organized, this makes the code more consistent and easier when a team works with it.

--

--