Find Interview Questions for Top Companies
Ques:- What is Redux?
Right Answer:
Redux is a state management library for JavaScript applications, commonly used with React. It helps manage the application state in a predictable way by using a single store and actions to update the state.
Comments
Admin Feb 3, 2020

Redux is an expected state container for JavaScript apps based on the Flux design pattern. Redux can be used mutually with React, or with any other view library. It is a little size (about 2kB) and has no dependency.

Ques:- What are the popular packages for animation?
Right Answer:
Some popular packages for animation in React are:

1. **React Spring** - A powerful library for creating animations using spring physics.
2. **Framer Motion** - A popular library for animations and gestures with a simple API.
3. **React Transition Group** - A low-level animation library for managing transitions in React components.
4. **GSAP (GreenSock Animation Platform)** - A robust library for high-performance animations.
5. **React Lottie** - A library for rendering animations created with Adobe After Effects using Lottie.
Comments
Admin Feb 3, 2020

React Motion and React Transition Group are popular animation packages in React ecosystem.

Ques:- Why is a component constructor called only once?
Right Answer:
A component constructor is called only once because it is invoked when the component is first created and mounted to the DOM. Subsequent updates to the component do not recreate it; instead, React updates the existing instance, which is why the constructor is not called again.
Comments
Admin Feb 3, 2020

React’s appeasement algorithm assumes that without any information to the contrary, if a custom component appears in the same place on consequent renders, it’s the same component as before, so reuses the previous instance moderately than making a new one.

Ques:- What will happen if you use set State() in constructor?
Right Answer:
Using `setState()` in the constructor will not work as expected because the component's state is not yet initialized. Instead, you should initialize the state directly in the constructor using `this.state = {}`.
Comments
Admin Feb 3, 2020

When you use set State(), then apart from assigning to the object state React also re renders the component and all its children. We would get error like this: Can only update a mounted or mounting component. Hence we need to use this. stateto initialize variables inside constructor.

Ques:- What is React DOM Server?
Right Answer:
React DOM Server is a package in React that allows you to render React components on the server side, generating HTML content that can be sent to the client. This helps improve performance and SEO by delivering fully rendered pages to the browser.
Comments
Admin Feb 3, 2020

The React DOM Server object enables us to render components to static markup . This object is mostly used for server-side rendering (SSR). The below methods can be used in both the server and browser environments:
renderToString()
renderToStaticMarkup()

Ques:- What are fragments?
Right Answer:
Fragments are a way to group multiple elements in React without adding an extra node to the DOM. They allow you to return multiple elements from a component without wrapping them in a div or another container. You can use `<React.Fragment>` or the shorthand `<>` syntax.
Comments
Admin Feb 3, 2020

Its mutual pattern in React which is used for a component to return multiple elements. Fragments let group a list of children without adding extra nodes to the DOM.

Ques:- What are the methods of React component lifecycle?
Right Answer:
The methods of the React component lifecycle are:

1. **Mounting**:
- `constructor()`
- `static getDerivedStateFromProps()`
- `render()`
- `componentDidMount()`

2. **Updating**:
- `static getDerivedStateFromProps()`
- `shouldComponentUpdate()`
- `render()`
- `componentDidUpdate()`

3. **Unmounting**:
- `componentWillUnmount()`

4. **Error Handling**:
- `static getDerivedStateFromError()`
- `componentDidCatch()`
Comments
Admin Feb 3, 2020

In the entire lifecycle of a React component, the following methods are used to accomplish the functions:
<ul>
<li><strong>componentWillMount()</strong> – On client and server side this function gets executed just before the rendering</li>
<li><strong>componentDidMount()</strong> – After first render it gets executed on the client side</li>
<li><strong>componentWillReceiveProps()</strong> – This function is invoked when the props are received from the parent class and another render is not being called.</li>
<li><strong>shouldComponentUpdate()</strong> – This Boolean function returns true or false as per situation like if the component needs to be updated then true is returned else false is returned</li>
<li><strong>thecomponentWillUpdate(</strong>) – It is called when rendering is not being called</li>
<li><strong>componentDidUpdate()</strong> – It is called just after when render function is called</li>
<li><strong>componentwillUnmount()</strong> – When a component gets un-mounted from DOM then this function is called</li>
</ul>

Ques:- What are controlled and uncontrolled components in React?
Right Answer:
Controlled components are React components that derive their form data from the component's state, meaning the component controls the input values. Uncontrolled components, on the other hand, manage their own state internally, and you access the input values using refs instead of state.
Comments
Admin Feb 3, 2020

<p id="de26" class="graf graf--p graf-after--p">This relates to stateful DOM components (form elements) and the difference:</p>

<ul class="postList">
<li id="e12c" class="graf graf--li graf-after--p">A <strong class="markup--strong markup--li-strong">Controlled Component</strong> is one that takes its current value through props and notifies changes through callbacks like onChange. A parent component “controls” it by handling the callback and managing its own state and passing the new values as props to the controlled component. You could also call this a “dumb component”.</li>
<li id="dee2" class="graf graf--li graf-after--li">A Uncontrolled Component is 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.</li>
</ul>
<p id="f500" class="graf graf--p graf-after--li">In most (or all) cases we should use controlled components.</p>

Ques:- How Virtual-DOM is more efficient than Dirty checking?
Right Answer:
The Virtual DOM is more efficient than dirty checking because it minimizes direct manipulation of the actual DOM by creating a lightweight copy of it. When changes occur, the Virtual DOM is updated first, and then a diffing algorithm determines the minimal number of updates needed to sync with the real DOM. This reduces performance overhead, as opposed to dirty checking, which continuously checks for changes and can lead to unnecessary re-renders and performance issues.
Comments
Admin Feb 3, 2020

In React, each of our components have a state. This state is like an observable. Essentially, React knows when to re-render the scene because it is able to observe when this data changes. Dirty checking is slower than observables because we must poll the data at a regular interval and check all of the values in the data structure recursively. By comparison, setting a value on the state will signal to a listener that some state has changed, so React can simply listen for change events on the state and queue up re-rendering.

The virtual DOM is used for efficient re-rendering of the DOM. This isn’t really related to dirty checking your data. We could re-render using a virtual DOM with or without dirty checking. In fact, the diff algorithm is a dirty checker itself.

We aim to re-render the virtual tree only when the state changes. So using an observable to check if the state has changed is an efficient way to prevent unnecessary re-renders, which would cause lots of unnecessary tree diffs. If nothing has changed, we do nothing.

Ques:- What is render() in React? And explain its purpose?
Right Answer:
`render()` is a lifecycle method in React that is responsible for describing what the UI should look like. It returns React elements, which are then rendered to the DOM. Its purpose is to define the structure and appearance of the component based on the current state and props.
Ques:- What is a higher order component?
Right Answer:
A higher-order component (HOC) is a function in React that takes a component and returns a new component, allowing for code reuse and the ability to add additional functionality or props to the wrapped component.
Comments
Admin Feb 3, 2020

A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API. They are a pattern that emerges from React’s compositional nature.

A higher-order component is a function that takes a component and returns a new component.

HOC’s allow you to reuse code, logic and bootstrap abstraction. HOCs are common in third-party React libraries. The most common is probably Redux’s connect function. Beyond simply sharing utility libraries and simple composition, HOCs are the best way to share behavior between React Components. If you find yourself writing a lot of code in different places that does the same thing, you may be able to refactor that code into a reusable HOC.

Ques:- What is react Context?
Right Answer:
React Context is a feature that allows you to share values (like state or functions) between components without having to pass props down manually through every level of the component tree. It provides a way to create global variables that can be accessed by any component within the Context Provider.
Comments
Admin Feb 3, 2020

React context is used to pass data through a tree of react components. Using context, we can share data globally between react components.

Ques:- What is Lifting state up in React?
Right Answer:
Lifting state up in React refers to the practice of moving state from a child component to a common parent component so that multiple child components can share and synchronize their state. This allows for better data management and communication between components.
Comments
Admin Feb 3, 2020

If several states need to share common data, we can lift the state up to its closest common ancestor. That means the child components will not hold any local state, instead, the parent component will hold the common state. This same state will be used by all parent and child components.

Ques:- Is it required the keys to be unique globally in a react application?
Right Answer:
No, keys in a React application only need to be unique among siblings in the same list, not globally.
Comments
Admin Feb 3, 2020

The keys should be unique among the sibling lists. But we can use the same keys if we have two different arrays.

Ques:- Is it a good practice to use item index as ‘key’ of list elements?
Right Answer:
No, it is not a good practice to use the item index as the 'key' of list elements in React.
Comments
Admin Feb 3, 2020

It is not a good practice to use the index of an item as the ‘key’ of the list elements. For example, if the order of the items changes, it will impact negatively and may cause issues with the react component state.

Ques:- What is the use of ‘key’ in react list?
Right Answer:
The 'key' in a React list is a unique identifier for each element in the list, which helps React efficiently update and re-render components by tracking changes, additions, or removals.
Comments
Admin Feb 3, 2020

Keys are used to provide each list element with a stable identity. The keys should be unique. The best practice is to use ‘id’ of the data as the key.

Ques:- Can we create one list without using ‘key’ in react?
Right Answer:
No, you cannot create a list in React without using a unique 'key' for each list item.
Comments
Admin Feb 3, 2020

We can create one list without ‘key’ for each element but it will cause issues with the react component rendering the list. Even on the browser console, it will throw one error.

Ques:- What are createElement and cloneElement?
Right Answer:
`createElement` is a React function used to create a React element from a type (like a string for a DOM element or a component) and props. `cloneElement` is a function that creates a new React element by cloning an existing element and merging it with new props.
Comments
Admin Feb 3, 2020

React.createElement is used to create one react element which will be used as a UI component. React.cloneElement is used to clone an element.

Ques:- What is a stateful component?
Right Answer:
A stateful component is a React component that manages its own state, allowing it to store and update data that can change over time, affecting how the component renders.
Comments
Admin Feb 3, 2020

‘stateful’ components are class components. They have one ‘state’ that is used for UI re-rendering.

Ques:- What is a stateless component?
Right Answer:
A stateless component is a React component that does not manage its own state and relies solely on props for rendering. It is typically a functional component that receives data and displays it without any internal state management.
Comments
Admin Feb 3, 2020

A stateless component has no ‘state’. It may take one props as an argument and returns a react element based on the values of the ‘props’. These components also don’t have a lifecycle.



React.js, often simply called React, is a powerful open-source JavaScript library developed by Facebook for building user interfaces (UIs) and single-page applications. Unlike a full-fledged framework, React focuses specifically on the “view” layer of an application, allowing developers to create highly dynamic and interactive user interfaces efficiently. It has become one of the most popular tools in the front-end development ecosystem due to its innovative component-based approach and a number of key architectural features.

At the core of React is the concept of a component. Instead of building an entire web page as a single, large HTML file, developers break down the UI into small, reusable, self-contained components. A component can be a simple button, a navigation bar, a search box, or a complex data table. Each component has its own logic and can manage its own state, making it easy to build complex UIs from smaller, manageable pieces. This modularity not only simplifies development but also makes the code more organized, reusable, and easier to maintain.

Another defining feature of React is its use of a Virtual DOM (Document Object Model). The DOM is the tree-like structure of HTML elements that a browser uses to render a web page. Directly manipulating the real DOM is often slow and inefficient. React creates a lightweight, in-memory representation of the DOM, known as the Virtual DOM. When an application’s state changes, React first updates the Virtual DOM, then uses an efficient algorithm to compare it with the previous state. It then calculates the most efficient way to update only the necessary parts of the real DOM, rather than re-rendering the entire page. This process, known as “reconciliation,” dramatically improves performance and makes React applications incredibly fast.

React also introduced JSX (JavaScript XML), a syntax extension for JavaScript that allows developers to write HTML-like code directly within their JavaScript files. While it may seem like a step backward to some, JSX makes the code more readable and intuitive, as it allows the logic and the UI markup for a component to live in the same place.

The advantages of using React are numerous. Its declarative nature means developers simply describe what the UI should look like, and React handles the rest. Its reusability and strong community support, coupled with a vast ecosystem of tools and libraries (like Redux for state management and React Router for navigation), make it a robust choice for everything from small personal projects to large-scale enterprise applications.

AmbitionBox Logo

What makes Takluu valuable for interview preparation?

1 Lakh+
Companies
6 Lakh+
Interview Questions
50K+
Job Profiles
20K+
Users