Ulloor News

ULLOOR
NEWS

Top 100 React.js Interview Questions and Answers

Share the Valuable Post

  1. What is React?
    React is a JavaScript library for building user interfaces, mainly used for creating single-page applications with a component-based architecture.
  2. What are the key features of React?
    • Component-based architecture
    • Virtual DOM
    • Unidirectional data flow
    • Reusable components
    • JSX syntax
  3. What is JSX?
    JSX (JavaScript XML) is a syntax extension for JavaScript that allows writing HTML-like code inside JavaScript.
  4. Why use React instead of plain JavaScript?
    React simplifies UI building with reusable components, improves performance with the Virtual DOM, and ensures predictable state management.
  5. What are components in React?
    Components are reusable, self-contained pieces of UI. They can be functional or class-based.
  6. What is the difference between functional and class components?
    • Functional components: Use functions, rely on Hooks for state and lifecycle.
    • Class components: Use ES6 classes, have lifecycle methods and state.
  7. What is the Virtual DOM?
    A lightweight copy of the real DOM. React updates the Virtual DOM first, then compares it with the previous version (diffing), and only updates the real DOM where necessary.
  8. What are props in React?
    Props (properties) are used to pass data from parent to child components. They are read-only.
  9. What is state in React?
    State is a built-in object in React that stores component data and controls how components render and behave.
  10. What is the difference between state and props?
  • Props: Passed from parent, immutable.
  • State: Managed inside the component, mutable.
  1. What are React lifecycle methods?
    Special methods in class components that run at different stages: mounting, updating, and unmounting.
  2. What are the lifecycle phases?
  • Mounting (constructor, render, componentDidMount)
  • Updating (shouldComponentUpdate, render, componentDidUpdate)
  • Unmounting (componentWillUnmount)
  1. What replaces lifecycle methods in functional components?
    React Hooks like useEffect.
  2. When is componentDidMount() used?
    Runs after the component is mounted; useful for API calls or subscriptions.
  3. When is componentWillUnmount() used?
    Runs before a component is removed; used for cleanup like removing event listeners.
  1. What are React Hooks?
    Functions that allow functional components to use state and lifecycle features.
  2. What are the most used Hooks?
  • useState
  • useEffect
  • useContext
  • useReducer
  • useRef
  • useMemo
  • useCallback
  1. What is useState?
    Hook to declare state variables in functional components.
  2. What is useEffect?
    Hook to handle side effects (API calls, DOM manipulation, subscriptions).
  3. Difference between useEffect and lifecycle methods?
    useEffect combines componentDidMount, componentDidUpdate, and componentWillUnmount.
  4. What is useContext?
    Hook that provides access to context data without using props drilling.
  5. What is useReducer?
    Alternative to useState for complex state logic. Works like Redux reducer.
  6. What is useRef?
    Hook that creates mutable references that don’t trigger re-renders (used for accessing DOM nodes).
  7. What is useMemo?
    Hook for memoizing values to avoid unnecessary recalculations.
  8. What is useCallback?
    Hook for memoizing functions to prevent unnecessary re-renders.
  1. What is React Fiber?
    A reimplementation of React’s reconciliation algorithm for better UI rendering.
  2. What is reconciliation in React?
    The process of updating the DOM by comparing the new Virtual DOM with the previous one.
  3. What is keys in React lists?
    Keys help React identify elements when rendering lists. They should be unique and stable.
  4. What is lifting state up?
    Moving state to a common ancestor to share data between child components.
  5. What is controlled vs uncontrolled components?
  • Controlled: Form data is handled by React state.
  • Uncontrolled: Form data handled by the DOM using ref.
  1. What is React Fragments?
    <React.Fragment> lets you return multiple elements without adding extra DOM nodes.
  2. What is React Portal?
    A way to render children into a DOM node outside the parent hierarchy.
  3. What is error boundary?
    Special component that catches JavaScript errors in child components.
  4. What is code splitting in React?
    Breaking code into smaller bundles to improve performance, often using React.lazy and Suspense.
  5. What is lazy loading in React?
    Dynamically loading components only when needed.
  1. What is Redux?
    A state management library based on a single store and predictable state updates.
  2. Core principles of Redux?
  • Single source of truth
  • State is read-only
  • Pure reducers handle state changes
  1. What are reducers in Redux?
    Pure functions that take current state and action, return new state.
  2. What are actions in Redux?
    Plain JavaScript objects describing what to do (type + payload).
  3. What is Redux middleware?
    Functions that run between dispatching an action and reaching the reducer.
  4. What is Redux Thunk?
    Middleware for handling async actions.
  5. Difference between Redux and Context API?
  • Redux: Better for large apps, middleware, debugging.
  • Context API: Lightweight, suitable for small/medium apps.
  1. What is the Redux store?
    Central place that holds the entire app state.
  2. How to connect React with Redux?
    Using react-redux with Provider, useSelector, and useDispatch.
  3. What is the difference between useSelector and useDispatch?
  • useSelector: Read state from store.
  • useDispatch: Dispatch actions to update store.
  1. How to optimize React performance?
  • Memoization (React.memo, useMemo, useCallback)
  • Code splitting
  • Avoid anonymous functions in render
  • Virtualization (e.g., react-window)
  • Using keys properly
  1. What is React.memo?
    Higher-order component that memoizes functional components to avoid unnecessary re-renders.
  2. What is shouldComponentUpdate()?
    Lifecycle method in class components to prevent unnecessary re-renders.
  3. What is useTransition in React 18?
    Hook for handling concurrent UI updates smoothly.
  4. What is suspense in React?
    Allows components to wait for async data before rendering.
  1. What is React Router?
    A library for handling routing in React applications.
  2. Difference between BrowserRouter and HashRouter?
  • BrowserRouter: Uses HTML5 history API.
  • HashRouter: Uses URL hash (#) for routing.
  1. What are Route and Switch?
  • Route: Renders a component for a path.
  • Switch: Renders only the first matching route.
  1. What is useNavigate in React Router v6?
    Hook to navigate programmatically.
  2. What is dynamic routing?
    Creating routes based on dynamic parameters (e.g., /user/:id).
  1. What is Jest?
    A JavaScript testing framework for unit tests.
  2. What is React Testing Library (RTL)?
    A testing library focused on testing React components from a user perspective.
  3. Difference between shallow and mount in Enzyme?
  • Shallow: Renders component without children.
  • Mount: Renders full DOM including children.
  1. How to test asynchronous code in React?
    Using async/await with waitFor in RTL.
  2. How to test React hooks?
    Use @testing-library/react-hooks or test components that use hooks.
  1. What is Concurrent Rendering?
    React can interrupt rendering for better UI responsiveness.
  2. What is useDeferredValue?
    Hook for deferring expensive calculations while keeping UI responsive.
  3. What is automatic batching in React 18?
    Groups multiple state updates into one re-render automatically.
  4. What is Server Components in React?
    Components rendered on the server to reduce client-side JavaScript.
  5. What is streaming SSR in React 18?
    Server-side rendering that streams HTML progressively.
  1. What is Flux architecture?
    Pattern where data flows in a single direction via actions, dispatcher, store, and views.
  2. What is the difference between React and Angular?
  • React: Library, virtual DOM, flexible.
  • Angular: Framework, two-way data binding, opinionated.
  1. What is the difference between React and Vue?
  • React: Uses JSX, flexible ecosystem.
  • Vue: Template-based, simpler learning curve.
  1. What are higher-order components (HOC)?
    Functions that take a component and return an enhanced component.
  2. What are render props?
    A technique where a component’s children is a function that controls rendering.
  3. What is prop drilling?
    Passing props through multiple levels of components unnecessarily.
  4. What is the solution for prop drilling?
    Context API or Redux.
  5. What is hydration in React?
    Attaching event listeners to server-rendered HTML on the client side.
  6. What is React Native?
    Framework for building mobile apps using React.
  7. What is difference between CSR and SSR?
  • CSR: Rendering happens in the browser.
  • SSR: Rendering happens on the server.
  1. How can you style components in React?
  • Inline styles
  • CSS stylesheets
  • CSS Modules
  • Styled-components (CSS-in-JS)
  • Tailwind CSS or other utility libraries
  1. What are CSS Modules in React?
    A way to scope CSS locally to components, avoiding global class conflicts.
  2. What are styled-components?
    A CSS-in-JS library allowing you to write CSS inside JavaScript using template literals.
  3. Difference between inline styles and CSS Modules?
  • Inline styles: Written directly in JSX.
  • CSS Modules: Written in .module.css files, scoped locally.
  1. Can React work without CSS?
    Yes, React only manages UI logic. Styling is optional, but usually CSS (or CSS-in-JS) is used.
  2. How to handle forms in React?
  • Controlled components (with state)
  • Uncontrolled components (with ref)
  1. What is two-way data binding in React?
    React does not have two-way binding by default. Instead, controlled components achieve similar results by updating state on input changes.
  2. How to validate forms in React?
  • Custom validation with state
  • Libraries like Formik or React Hook Form
  1. What is synthetic event in React?
    A wrapper around browser events that works the same across different browsers.
  2. How to handle multiple inputs in React forms?
    Use name attributes and update state dynamically with [e.target.name]: e.target.value.
  1. What is Next.js?
    A React framework for server-side rendering, static site generation, and routing.
  2. What is Gatsby.js?
    A React framework for building static websites with GraphQL.
  3. What is React Query?
    A library for fetching, caching, and synchronizing server state in React.
  4. What is Recoil in React?
    A state management library for managing global state with atoms and selectors.
  5. What is React DevTools?
    A browser extension for debugging React components, props, and state.
  1. How do you build a React app for production?
    Using npm run build or yarn build which creates optimized static files.
  2. How to deploy a React app?
  • Netlify
  • Vercel
  • GitHub Pages
  • AWS, Azure, or Firebase hosting
  1. What is tree shaking in React?
    Process of removing unused code from the final bundle.
  2. What is source maps in React?
    Files that map minified code to original source code for debugging.
  3. What are environment variables in React?
    Variables like API keys stored in .env files, accessed via process.env.
  1. How to prevent XSS (Cross-Site Scripting) in React?
    React escapes content by default. For raw HTML, use dangerouslySetInnerHTML carefully.
  2. What is dangerouslySetInnerHTML?
    A React prop that lets you set HTML directly into the DOM. It must be sanitized to avoid XSS.
  3. How to secure API keys in React apps?
    Do not expose keys in frontend. Use backend proxy or serverless functions.
  4. What is CSRF and how to prevent it in React apps?
    Cross-Site Request Forgery; prevent using CSRF tokens and same-site cookies.
  5. What are best security practices in React?
  • Escape data properly
  • Use HTTPS
  • Avoid inline scripts
  • Use authentication & authorization properly
  • Regular dependency updates

Share the Valuable Post
Scroll to Top