Basic ReactJS Interview Questions
1. What are the features of React?
JSX: JSX is a syntax extension to JavaScript. It is used with React to describe the user interface’s appearance. Using JSX, we can write HTML structures in the same file that contains JavaScript code.
Components: Components are the building blocks of any React application, and a single app usually consists of multiple components. It splits the user interface into independent, reusable parts that can be processed separately.
Virtual DOM: React keeps a lightweight representation of the real DOM in memory, known as the virtual DOM. When an object’s state changes, the virtual DOM changes only that object in the real DOM rather than updating all the objects.
One-way data-binding: React’s one-way data binding keeps everything modular and fast. A unidirectional data flow means that when designing a React app, you often nest child components within parent components.
High performance: React updates only those components that have changed rather than all the components simultaneously, resulting in faster web applications.
2. What is JSX?
JSX is a syntax extension of JavaScript. It is used with React to describe the user interface’s appearance. Using JSX, we can write HTML structures in the same file that contains JavaScript code.
render(){ return( <> <h1>This is JSX</h1> </> ) }
3. Can web browsers read JSX directly?
- Web browsers cannot read JSX directly. They are built only to read regular JS objects, and JSX is not a regular JavaScript object.
- A web browser can only read a JSX file if transformed into a regular JavaScript object. We use Babel for this.
4. What is the virtual DOM?
DOM stands for Document Object Model. It represents an HTML document with a logical tree structure. Each branch of the tree ends in a node, which contains objects.React keeps a lightweight representation of the real DOM in memory, known as the virtual DOM. When an object’s state changes, the virtual DOM changes only that object in the real DOM rather than updating all the objects. The following are some of the most frequently asked react interview questions.
5. Why use React instead of other frameworks, like Angular?
- Easy creation of dynamic applications: React makes it easier to create dynamic web applications because it requires less coding and more functionality, whereas code tends to get complex very quickly with JavaScript applications.
- Improved performance: React uses virtual DOM, which makes web applications perform faster. Virtual DOM compares its previous state and updates only those components in the real DOM, whose states have changed, rather than updating all the components — like conventional web applications.
- Reusable components: Components are the building blocks of any React application, and a single app usually consists of multiple components. These components have logic and controls and can be reused throughout the application, dramatically reducing development time.
- Unidirectional data flow: React follows a unidirectional data flow. When designing a React app, we often nest child components within parent components. Since the data flows in a single direction, debugging errors and knowing where the problem occurs in an application become easier.
- Dedicated tools for easy debugging: Facebook has released a Chrome extension for debugging React applications. This makes the process of debugging React web applications faster and easier.
6. What is an event in React?
An event is an action a user or system may trigger, such as pressing a key or clicking a mouse.
- React events are named using camelCase rather than lowercase in HTML.
- With JSX, you pass a function as the event handler rather than a string in HTML.
In React, events are a way to handle user interactions, such as clicks, key presses, form submissions, etc. React provides a synthetic event system, which is a cross-browser wrapper around the browser’s native event system. This ensures that events behave consistently across different browsers.
7. What are synthetic events in React?
- Synthetic events combine the response of different browsers’ native events into one API, ensuring consistency across browsers.
- The application is consistent regardless of the browser in which it is running. Here, preventDefault is a synthetic event.
8. How do you write comments in React?
There are two ways in which we can write comments:
- Single-line comments
- Multi-line comments
9. What is an arrow function and how is it used in React?
- An arrow function is a short way of writing a function to React.
- It is unnecessary to bind ‘this’ inside the constructor when using an arrow function. This prevents bugs caused by the use of ‘this’ in React callbacks.
10. What are React Hooks?
React Hooks are functions introduced in React 16.8 that allow you to use state and lifecycle features in functional components. Hooks like useState, useEffect, and useContext make it easier to manage component logic without using classes, improving readability and reusability.
11. What is useState, and how does it work?
useState is a hook that allows you to add state to functional components. It takes an initial state value and returns an array with two elements: the current state and a function to update it. useState re-renders the component when the state is updated.
12. What is useEffect, and how does it differ from lifecycle methods in class components?
useEffect is a hook for performing side effects in functional components, like fetching data or setting up subscriptions. It combines the behavior of componentDidMount, componentDidUpdate, and componentWillUnmount in class components, allowing effects to run after render and clean up when dependencies change.
13. What is Memoization in React?
Memoization is an optimization technique that saves the result of expensive function calls and reuses it when the same inputs occur. React.memo and useMemo prevent unnecessary re-renders and calculations by caching results based on dependencies.
14 What is Prop Drilling and how do you avoid it?
Prop drilling occurs when props are unnecessarily passed through multiple component layers to reach a nested component. You can avoid it using Context API, which allows you to share the state between deeply nested components without manually passing props down.