0% found this document useful (0 votes)
47 views8 pages

Untitled Document

The document is a comprehensive guide for React interview preparation, covering essential concepts such as components, state, props, and the Virtual DOM. It includes detailed explanations of features, event handling, and best practices, along with code examples. The content is structured as a list of questions and answers, making it a useful resource for candidates preparing for React-related interviews.

Uploaded by

42khan0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Untitled Document

The document is a comprehensive guide for React interview preparation, covering essential concepts such as components, state, props, and the Virtual DOM. It includes detailed explanations of features, event handling, and best practices, along with code examples. The content is structured as a list of questions and answers, making it a useful resource for candidates preparing for React-related interviews.

Uploaded by

42khan0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

## React Interview Preparation (Q1 - Q60)

---

### 1. What is React?

React is a JavaScript library developed by Facebook for building user interfaces, particularly
for single-page applications. It allows developers to create reusable UI components and
manage the state of those components efficiently. React uses a virtual DOM to optimize
performance and make UI updates more efficient.

---

### 2. What is the history behind React’s evolution?

React was created by Jordan Walke, a software engineer at Facebook. It was first deployed
on Facebook's newsfeed in 2011 and later on Instagram in 2012. React was open-sourced in
2013. Over time, React has evolved significantly, introducing features like JSX, React Fiber,
and Hooks (in version 16.8), making it more functional and performant.

---

### 3. What are the major features of React?

* **JSX (JavaScript XML)**


* **Components**: Reusable and encapsulated pieces of UI
* **Virtual DOM**: For fast rendering
* **One-way data binding**
* **Declarative UI**
* **React Hooks**: Functional state and side-effect management

---

### 4. What is JSX?

JSX stands for JavaScript XML. It is a syntax extension for JavaScript that looks similar to
HTML. JSX allows developers to write HTML-like code inside JavaScript, which is then
transformed to [Link]() calls. It enhances code readability and helps in writing
components more intuitively.

---

### 5. What is the difference between an Element and a Component?

* **Element**: A plain object representing a DOM node or component. It is the smallest


building block in React.
* **Component**: A function or class that optionally accepts input (props) and returns a
React element. Components can contain multiple elements.
---

### 6. How do you create components in React?

React components can be created in two ways:

* **Functional Components**:

```jsx
function Welcome(props) {
return <h1>Hello, {[Link]}</h1>;
}
```
* **Class Components**:

```jsx
class Welcome extends [Link] {
render() {
return <h1>Hello, {[Link]}</h1>;
}
}
```

---

### 7. When should you use a Class Component over a Function Component?

Class components are traditionally used when a component needs to manage its own state
or lifecycle methods. However, with the introduction of Hooks, function components can now
manage state and side-effects, making class components less necessary in modern React
applications.

---

### 8. What are Pure Components?

Pure components are React components that do not re-render if the props and state haven't
changed. [Link] performs a shallow comparison of props and state and
avoids unnecessary rendering, optimizing performance.

---

### 9. What is state in React?

State is a built-in object that stores property values that belong to a component. When the
state of a component changes, the component re-renders. State is mutable and managed
within the component.
---

### 10. What are props in React?

Props (short for properties) are read-only attributes used to pass data from parent to child
components. They make components reusable by allowing customization.

---

### 11. What is the difference between state and props?

* **Props**: Passed from parent to child. Immutable by the child.


* **State**: Managed within the component. Mutable and controls the component's behavior.

---

### 12. What is the difference between HTML and React event handling?

In React:

* Event names are written in camelCase (e.g., `onClick` instead of `onclick`).


* Event handlers are passed as functions rather than strings.

---

### 13. What are synthetic events in React?

Synthetic events are cross-browser wrappers around the browser’s native events. They have
the same interface as native events but work identically across all browsers.

---

### 14. What are inline conditional expressions?

Conditional expressions used directly within JSX to render components or elements


conditionally.
Example:

```jsx
{isLoggedIn ? <Dashboard /> : <Login />}
```

---

### 15. What is the "key" prop and what is its benefit when used in arrays of elements?
The key prop is a special attribute used to identify elements in a list. It helps React
determine which items have changed, are added, or are removed, optimizing rendering
performance.

---

### 16. What is the Virtual DOM?

A lightweight copy of the real DOM that React uses to batch and optimize updates. Changes
are first made to the virtual DOM, and React uses a diffing algorithm to apply only necessary
updates to the real DOM.

---

### 17. How does the Virtual DOM work?

* React renders UI to a virtual DOM.


* On state/props change, React re-renders to a new virtual DOM.
* It compares the new virtual DOM with the previous one (diffing).
* Only the changed parts are updated in the real DOM.

---

### 18. What is the difference between Shadow DOM and Virtual DOM?

* **Shadow DOM**: A browser technology for encapsulating DOM and style.


* **Virtual DOM**: A concept used by React to optimize rendering.

---

### 19. What is React Fiber?

React Fiber is the reimplementation of React’s core algorithm for rendering. It enables
incremental rendering, prioritization, and better error handling.

---

### 20. What is the main goal of React Fiber?

To enable smooth, interruptible rendering and improve responsiveness and performance by


breaking rendering into units of work.

---

### 21. What are controlled components?

Controlled components are form elements whose value is controlled by React state. Any
user input updates the state, which in turn updates the input's value.
---

### 22. What are uncontrolled components?

Uncontrolled components manage their own state internally using the DOM rather than
React state. They are accessed using refs.

---

### 23. What is the difference between createElement and cloneElement?

* `createElement`: Creates a new React element.


* `cloneElement`: Clones an existing element and optionally overrides its props.

---

### 24. What is Lifting State Up in React?

When multiple components need to share the same state, the state is lifted up to their
closest common ancestor and passed down via props.

---

### 25. What are Higher-Order Components?

A Higher-Order Component (HOC) is a function that takes a component and returns a new
component with enhanced behavior or additional props.

---

### 26. What is the children prop?

The `children` prop is a special prop that contains any nested elements or components
inside a parent component.

---

### 27. How do you write comments in React?

Comments in JSX are written using curly braces and multiline JavaScript comment syntax:

```jsx
{/* This is a comment */}
```

---
### 28. What is reconciliation?

Reconciliation is the process through which React updates the DOM by comparing the new
virtual DOM with the previous one and determining the minimal set of changes.

---

### 29. Does the lazy function support named exports?

No. `[Link]()` only supports default exports. You must wrap named exports in a
component that re-exports them as default.

---

### 30. Why does React use className instead of the class attribute?

Because `class` is a reserved keyword in JavaScript, React uses `className` to define CSS
classes on elements.

---

### 31. What are Fragments?

Fragments let you group multiple elements without adding an extra node to the DOM. They
help avoid unnecessary wrappers.

```jsx
<>
<Child1 />
<Child2 />
</>
```

---

### 32. Why are Fragments better than container divs?

Fragments prevent adding extra DOM nodes, which helps in maintaining cleaner markup
and avoids CSS or accessibility issues from unnecessary wrappers.

---

### 33. What are portals in React?

Portals provide a way to render children into a DOM node outside the parent component
hierarchy. Useful for modals, tooltips, etc.

```jsx
[Link](child, container)
```

---

### 34. What are stateless components?

Components that do not manage any state of their own. They simply receive props and
render UI.

---

### 35. What are stateful components?

Components that maintain and manage their own internal state using `useState` or
`[Link]`.

---

### 36. How do you apply validation to props in React?

Using `PropTypes`:

```jsx
[Link] = {
name: [Link]
};
```

Or using TypeScript for static type checking.

---

### 37. What are the advantages of React?

* Fast performance via Virtual DOM


* Component reusability
* Rich ecosystem and tooling
* One-way data flow ensures predictability
* Works well with other libraries and frameworks

---

### 38. What are the limitations of React?

* JSX syntax may be confusing initially


* Only covers the UI layer
* SEO challenges (without SSR)
* Fast-moving ecosystem requires constant learning

---

### 39. What are the recommended ways for static type checking?

* TypeScript
* Flow
* PropTypes (runtime)

---

### 40. What is the use of the react-dom package?

`react-dom` provides DOM-specific methods for React, like `[Link]()` to render


components, and `[Link]()` for portal support.

---

(Continued...)

Common questions

Powered by AI

JSX, or JavaScript XML, enhances code readability and allows developers to write HTML-like structures within JavaScript, facilitating intuitive component creation. However, JSX syntax might initially be confusing to developers unfamiliar with it and can complicate the code when overused with inline styling or logic .

Class components should be considered in legacy codebases or when dealing with third-party libraries not updated for hooks. Though traditionally used for managing state and lifecycle, function components can fully replace them using hooks like useState and useEffect. Class components can be useful for developers not yet comfortable with hooks or for readability in complex lifecycle management .

React uses a lightweight copy of the real DOM known as the Virtual DOM to optimize UI updates. When a component's state or props change, React first updates a new virtual DOM representation, not the real DOM directly. React then performs a diffing algorithm to compare this updated virtual DOM against the previous version. Only the parts of the actual DOM that have changed are then updated, making UI updates more efficient and reducing browser workload .

Higher-Order Components (HOCs) are advanced techniques in React for reusing component logic. They take a component and return a new component with enhanced features or additional props. HOCs enable code reuse and logic abstraction, allowing multiple components to share similar functionalities such as authentication, data fetching, or event handling .

React Fiber is a reimplementation of React's core algorithm, transforming how React performs rendering by breaking tasks into prioritized units of work. It improves responsiveness and error handling by allowing rendering to be interrupted and resumed, facilitating animations and complex UI updates without blocking the main thread .

The key attribute ensures that elements in lists are rendered and re-ordered correctly, enhancing performance by helping React recognize which items have changed or remained unchanged. Without proper keys, React may inefficiently re-render all items, while appropriate use of keys makes component updates more predictable and performant .

Portals in React allow components to be rendered outside their parent hierarchy, which is especially useful for user interface elements like modals or tooltips that need to visually break out of their parent components. This provides advantages in z-index fixes or positioning, while maintaining the benefits of React's event bubbling and state management .

Lifting state up in React involves moving the state to the nearest common ancestor of components that require access to it. This practice promotes a single source of truth for the state, making it easier to share state across multiple components and keeping state management centralized and efficient. It also clarifies data flow, as the state is passed down as props .

Uncontrolled components rely on the DOM for managing their state rather than React's state management. This can lead to potential issues in predictability and consistency of UI behavior. In contrast, controlled components use React state to dictate form values, offering better control over input data and form submissions, at the cost of increased complexity in managing state transitions .

The Shadow DOM is a browser standard that encapsulates a section of the DOM and its styles for better modularity and style encapsulation. The Virtual DOM, used by React, is a programming concept where a virtual representation of the UI is kept in memory and synchronized with the real DOM using efficient updates. While Shadow DOM addresses component isolation and style conflicts, Virtual DOM offers efficient updates of the user interface by minimizing real DOM manipulations .

You might also like