Skip to content

Interview Question (React) ‐ Part 1

Anurag Mishra edited this page Jun 7, 2024 · 2 revisions

Interview Basic Questions

1. Explain React Architecture

  • React follows a component-based architecture where UIs are built using reusable components.
  • It employs a unidirectional data flow, where data flows from parent to child components.
  • React uses a virtual DOM to efficiently update the actual DOM, reducing the number of manipulations needed.
  • Components can have their own state, and when the state changes, React automatically re-renders the components.

2. Explain Life Cycle Methods of React JS

  • React components have lifecycle methods that allow you to perform actions at different stages of a component's existence. These methods include:
    • componentDidMount
    • componentDidUpdate
    • componentWillUnmount
  • They help in managing component initialization, updates, and cleanup.

3. Explain the Basic Setup and Advanced Configurations for a React Application

  • Basic Setup:
    • Installing dependencies like react and react-dom
    • Setting up a build system using tools like Webpack or Create React App
    • Creating components
  • Advanced Configurations:
    • Setting up routing
    • State management with Redux
    • Styling with CSS preprocessors or CSS-in-JS libraries
    • Optimizing performance

4. Explain the Key Concepts of Redux and How Middleware Can Be Used in Redux

  • Key Concepts:
    • Single immutable state tree
    • Actions that describe state changes
    • Reducers that specify how actions transform the state
    • Store that holds the state
  • Middleware:
    • Intercepts dispatched actions, allowing you to perform asynchronous tasks, logging, or other side effects before they reach the reducers.

5. Explain ES6 Features

  • Arrow Functions
  • Classes
  • Template Literals
  • Destructuring Assignment
  • Spread/Rest Operators
  • let and const for block-scoped variables
  • These features enhance readability, maintainability, and expressiveness of JavaScript code.

6. How to Make API Calls in a React Application?

  • API calls can be made using:
    • fetch API
    • Axios
    • Libraries like react-query or swr
  • Typically, you make API calls within lifecycle methods like componentDidMount or using hooks like useEffect.

7. How to Interact or Communicate with a Node.js Server in a React Application?

  • Communicate using HTTP requests (GET, POST, PUT, DELETE) via libraries like fetch or Axios.
  • Server-side code in Node.js can handle these requests using frameworks like Express.js, and responses can be sent back to React in JSON format.

8. How Does Virtual DOM Work in React?

  • Virtual DOM is a lightweight copy of the actual DOM in memory.
  • When the state of a React component changes, React compares the current Virtual DOM with a previous version, identifies the differences (reconciliation), and updates only the necessary parts of the actual DOM, minimizing performance overhead.

9. What is JSX?

  • JSX (JavaScript XML) is a syntax extension for JavaScript used in React to describe what the UI should look like.
  • Allows writing HTML-like code within JavaScript, making it easier to write and understand React components.

10. Explain the Types of Components in React

  • Functional Components:
    • JavaScript functions that accept props and return JSX.
  • Class Components:
    • ES6 classes that extend React.Component and have additional features like state and lifecycle methods.

11. What is the Difference Between State and Props?

  • Props:
    • Passed from parent to child components
    • Immutable within the child component
  • State:
    • Managed within the component
    • Can be changed using setState
  • Props are used to pass data from parent to child, while state is used for managing internal component data.

12. What is a Controlled Component in React?

  • A controlled component in React is a component whose value is controlled by React state.
  • Changes to the component's value are handled by React state and are reflected in the UI.
  • Typically used for form elements like input fields.

13. What is a Higher-Order Component (HOC)? Explain with an Example

  • A higher-order component in React is a function that takes a component as input and returns a new component with additional functionalities.
  • Example: withAuthentication HOC could wrap a component and ensure that it's only rendered if the user is authenticated.

14. Explain Redux

  • Redux is a predictable state container for JavaScript applications.
  • Maintains the application state in a single immutable state tree
  • State mutations are performed by dispatching actions
  • Reducers specify how the state changes in response to actions
  • Facilitates predictable data flow and centralized state management.

15. Explain the New Features Introduced in HTML5

  • Semantic Elements: <header>, <footer>, <nav>, <article>, <section>, <main>
  • Multimedia Elements: <audio>, <video>
  • Form Enhancements: <input type="date">, <input type="email">, <input type="color">
  • Canvas for drawing graphics
  • Local Storage and Session Storage

16. Which Tools are Used in Your Organization to Build Applications?

  • Version control systems: Git
  • Project management tools: Jira, Trello
  • IDEs: Visual Studio Code, WebStorm
  • Build tools: Webpack, Parcel
  • Testing frameworks: Jest, Cypress
  • Deployment tools: Docker, Kubernetes

17. What is connect in Redux?

  • connect is a function provided by the React Redux library that connects React components to the Redux store.
  • Accepts mapStateToProps and mapDispatchToProps functions as arguments to specify how the component should interact with the store's state and actions.

18. Differentiate Between slice and splice

  • slice:
    • Returns a shallow copy of a portion of an array into a new array without modifying the original array.
  • splice:
    • Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

19. Differentiate Between map and forEach

  • map:
    • Iterates over an array and returns a new array with the results of calling a provided function on every element in the calling array.
  • forEach:
    • Iterates over an array and executes a provided function once for each array element, but it does not return anything.

20. What is Redux-saga?

  • Redux-saga is a middleware library for Redux that aims to make application side effects (like asynchronous data fetching and impure functions) easier to manage, more efficient to execute, easy to test, and better at handling failures.
  • Uses ES6 generators to write asynchronous code that looks synchronous.

21. Differentiate Between let, var, and const

  • var:
    • Function-scoped
    • Can be redeclared and reassigned.
  • let:
    • Block-scoped
    • Can be reassigned but not redeclared in the same scope.
  • const:
    • Block-scoped
    • Cannot be reassigned or redeclared after initialization.

22. What is setState in React?

  • setState is a method provided by React's Component class used to update the state of a component.
  • Accepts an object that represents the new state or a function that returns an object, and schedules a re-render of the component with the updated state.

23. Define Higher-Order Component (HOC) in React

  • A Higher-Order Component (HOC) in React is a function that takes a component and returns a new component with additional functionalities.
  • Commonly used for code reuse, cross-cutting concerns like authentication or logging, and adding state or behavior to components.

24. What is PureComponent in React?

  • PureComponent is a class component in React that extends Component and implements a shouldComponentUpdate method with a shallow prop and state comparison.
  • Ensures that the component only re-renders when its props or state have changed.

25. What is Rendering in React JS?

  • Rendering in React refers to the process of converting React elements into a DOM tree and displaying them in the browser.
  • When the state or props of a component change, React re-renders the component and updates the DOM efficiently using its virtual DOM algorithm.

26. How to Connect React to Redux?

  • React can be connected to Redux using the connect function provided by the React Redux library.

  • Wrap your root component with the Provider component from React Redux to make the Redux store available to all components.

  • Use the connect function to connect individual components to the store.

27. Difference Between Stateful and Stateless Components in React

  • Stateful Components (Class Components):
    • Have their own internal state managed by React
    • Can change their state using setState.
  • Stateless Components (Functional Components):
    • Do not have state
    • Typically used for presentational purposes, receiving data via props and rendering UI.

28. How to Update State in React?

  • State in React can be updated using the setState method provided by React's Component class.
  • You can pass an object containing the updated state properties or a function that returns such an object.
  • React will then schedule a re-render of the component with the updated state.

29. Difference Between Redux and Flux

  • Redux:
    • Inspired by Flux architecture
    • Simplifies it by providing a single store, unidirectional data flow, and immutable state
    • Introduces concepts like reducers, actions, and middleware.
  • Flux:
    • Allows multiple stores
    • Supports bidirectional data flow.

30. How Would You Conditionally Render a Component in the Render Function?

  • You can conditionally render a component in the render function by using JavaScript's conditional (ternary) operator or the logical && operator.
  • Example:
    render() {
      const isLoggedIn = this.props.isLoggedIn;
      return (
        <div>
          {isLoggedIn ? <LoggedInComponent /> : <LoggedOutComponent />}
        </div>
      );
    }
Clone this wiki locally