React Interview Questions and Answers – Part 3

Frequently asked React Interview Questions and Answers for freshers as well as experienced developers.

React Interview Questions

List of React Interview Questions and Answers

  1. What is Refs in React?
  2. How to create Refs in React?
  3. What are forward refs in React?
  4. What is virtual DOM?
  5. How virtual DOM is working in React?
  6. Difference between Controlled and Uncontrolled Components in React
  7. What are Higher Order Components (HOC) in React?
  8. What can you do with HOC?
  9. What is Lifting State Up in React?
  10. How to write comments in React?

1. What is Refs in React?

Refs is an attribute of the DOM elements. It’s used to return a reference of the element. It should be avoided in most cases, however, it can be useful when you need direct access to the DOM element or an instance of a component.

Refs are generally used for the following purposes:

  • Managing focus, text selection, or media playback.
  • Triggering imperative animations.
  • Integrating with third-party DOM libraries.

2. How to create Refs in React?

You can create Refs using three different methods.

Using React.createRef():

After rendering an element you can access ref via the current attribute.

Using callback ref:

Using useRef() hook:

3. What are forward refs in React?

The Ref forwarding is a feature that lets some components take a ref they receive, and pass it further down to a child.

4. What is virtual DOM?

The virtual DOM is an in-memory representation of the real DOM elements generated by React components before any changes are made to the page. 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.

5. How virtual DOM is working in React?

The Virtual DOM works in three simple steps.

  • Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation.
  • Then the difference between the previous DOM representation and the new one is calculated.
  • Once the calculations are done, the real DOM will be updated with only the things that have actually changed.

6. Difference between Controlled and Uncontrolled Components in React

Maintain State:
Controlled Components – Do not maintain their own state.
Uncontrolled Components – Maintain their own state.

Data controll:
Controlled Components – Data is controlled by parent component.
Uncontrolled Components – Data is controlled by DOM.

Current value:
Controlled Components – Takes in current values through props and notifies changes via callbacks.
Uncontrolled Components – Refs are used to get their current value.

7. What are Higher Order Components (HOC) in React?

  • They are Custom components which wraps another component.
  • They accept dynamically provided child components.
  • They do not modify the input component.
  • They do not copy any behavior from the input component.
  • They are Pure functions.

8. What can you do with HOC?

  • Can reuse the code, logic and bootstrap abstraction.
  • Can render high jacking.
  • State abstraction and manipulation.
  • Can manipulate the props.

9. What is Lifting State Up in React?

When several components need to share the same changing data then it is recommended to lift the shared state up to their closest common ancestor. That means if two child components share the same data from its parent, then move the state to parent instead of maintaining local state in both of the child components.

10. How to write comments in React?

The comments in React/JSX are wrapped in curly braces.

Single-line comments

Multi-line comments

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *