Basic React Concepts
- What is React?
- React is a JavaScript library for building user interfaces, particularly single-page applications where data can change over time without reloading the page.
- What are the key features of React?
- Components: Reusable UI elements.
- Virtual DOM: Efficient updating of the UI.
- JSX: Syntax extension that allows writing HTML in JavaScript.
- Unidirectional Data Flow: Data flows from parent to child components.
- What is JSX?
- JSX is a syntax extension for JavaScript that allows you to write HTML-like code in your JavaScript files. It gets transpiled to JavaScript by tools like Babel.
- What is the difference between Element and Component?
- An element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. A component is a function or class that optionally accepts inputs (props) and returns a React element.
- What are props in React?
- Props (short for properties) are a way of passing data from parent to child components. They are read-only and help in making components reusable.
- What is state in React?
- State is a built-in React object that is used to contain data or information about the component. A component’s state can change over time, usually as a result of user events.
Component Lifecycle
- What are the lifecycle methods in React?
- Mounting:
componentDidMount
- Updating:
componentDidUpdate
- Unmounting:
componentWillUnmount
- Error Handling:
componentDidCatch
- Mounting:
- What is the difference between
componentDidMount
andcomponentDidUpdate
?componentDidMount
is called after the component is mounted to the DOM, whilecomponentDidUpdate
is called after the component’s updates are flushed to the DOM.
State Management
- What is the difference between state and props?
- State is managed within the component and can be changed, while props are passed to the component and are read-only.
- How do you lift state up in React?
- Lifting state up is a pattern where you move the state to a common ancestor component so that it can be shared among sibling components.
Hooks
- What are React Hooks?
- Hooks are functions that let you use state and other React features in functional components. Examples include
useState
,useEffect
, anduseContext
.
- Hooks are functions that let you use state and other React features in functional components. Examples include
- What is the
useState
hook?useState
is a hook that lets you add state to functional components. It returns an array with two elements: the current state value and a function to update it.
- What is the
useEffect
hook?useEffect
is a hook that lets you perform side effects in functional components. It can be used for data fetching, subscriptions, or manually changing the DOM.
Intermediate React Questions
- What is the difference between class components and functional components?
- Class components use ES6 classes and have lifecycle methods, while functional components are simpler and use hooks for state and lifecycle features.
- What is the purpose of
React.Fragment
?React.Fragment
allows you to group multiple elements without adding an extra node to the DOM. It’s useful when you need to return multiple elements from a component.
- What is the significance of
key
prop in lists?- The
key
prop helps React identify which items have changed, been added, or been removed in a list. It improves performance by minimizing re-renders.
- The
- What is the difference between
useState
anduseReducer
?useState
is used for simple state management, whileuseReducer
is better for complex state logic or when the next state depends on the previous state.
- What is the difference between
useEffect
anduseLayoutEffect
?useEffect
runs after the render is committed to the screen, whileuseLayoutEffect
runs synchronously after all DOM mutations. UseuseLayoutEffect
when you need to measure the DOM or perform DOM mutations before the browser paints.
- How do you handle events in React?
- Events in React are written in camelCase (e.g.,
onClick
) and are passed as functions. React uses synthetic events for cross-browser compatibility.
- Events in React are written in camelCase (e.g.,
- What is prop drilling, and how can you avoid it?
- Prop drilling is the process of passing props through multiple levels of components. It can be avoided using Context API or state management libraries like Redux.
- What is the difference between
React.memo
anduseMemo
?React.memo
is a higher-order component used to memoize functional components, whileuseMemo
is a hook used to memoize values within a component.
- What is the purpose of
forwardRef
in React?forwardRef
allows you to pass aref
from a parent component to a child component, which is useful for accessing DOM elements or component instances.
- What is the difference between
createElement
andcloneElement
?createElement
creates a new React element, whilecloneElement
clones an existing element and allows you to add or override its props.
Advanced React Questions
- What is React’s reconciliation algorithm?
- Reconciliation is the process by which React updates the DOM. It uses a diffing algorithm to compare the Virtual DOM with the real DOM and applies the minimal set of changes.
- What are render props?
- Render props is a pattern where a component accepts a function as a prop and uses it to render content. It’s useful for sharing logic between components.
- What is the difference between
useCallback
anduseMemo
?useCallback
memoizes a function, whileuseMemo
memoizes a value. Both are used to optimize performance by preventing unnecessary re-renders.
- How does React handle server-side rendering (SSR)?
- React can render components on the server using
ReactDOMServer
. This improves performance and SEO by sending pre-rendered HTML to the client.
- React can render components on the server using
- What is the purpose of
React.StrictMode
?React.StrictMode
is a tool for highlighting potential problems in an application. It helps identify unsafe lifecycle methods, legacy APIs, and other issues.
- What is the difference between
useContext
and Redux?useContext
is a built-in React hook for sharing state across components, while Redux is a state management library with more advanced features like middleware and dev tools.
- What are React Portals, and when would you use them?
- Portals allow you to render a child component outside its parent DOM hierarchy. They are useful for modals, tooltips, or dropdowns that need to break out of their container.
- What is the purpose of
Error Boundaries
in React?- Error boundaries are components that catch JavaScript errors in their child component tree, log them, and display a fallback UI instead of crashing the app.
- What is the difference between
React.lazy
andSuspense
?React.lazy
is used to lazily load components, whileSuspense
is used to specify a fallback UI (e.g., a loading spinner) while the lazy component is being loaded.
- How do you test React components?
- React components can be tested using tools like Jest for unit testing and React Testing Library or Enzyme for component testing.
- What is the Context API?
- The Context API is a way to pass data through the component tree without having to pass props down manually at every level.
- What is Redux and how does it work with React?
- Redux is a state management library that can be used with React to manage the state of an application in a predictable way. It uses a single global store to hold the state.
- What are Higher-Order Components (HOCs)?
- A Higher-Order Component is a function that takes a component and returns a new component. It is used to share common functionality between components.
- What are React Portals?
- Portals provide a way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
Performance Optimization
- How do you optimize performance in React?
- Use
React.memo
for functional components. - Use
PureComponent
for class components. - Implement
shouldComponentUpdate
to prevent unnecessary re-renders. - Use lazy loading and code splitting with
React.lazy
andSuspense
.
- Use
- What is the Virtual DOM?
- The Virtual DOM is a lightweight copy of the actual DOM. React uses it to optimize updates by comparing the Virtual DOM with the real DOM and only updating the parts that have changed.
Miscellaneous
- What are keys in React and why are they important?
- Keys are special attributes used to identify which items have changed, are added, or are removed. They help React identify which elements need to be updated, improving performance.
- What is the difference between controlled and uncontrolled components?
- Controlled components have their state controlled by React, while uncontrolled components maintain their own state internally.
- What is React Router?
- React Router is a library for routing in React applications. It allows you to define routes and navigate between different components.
- What are error boundaries in React?
- Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI.
- What is the significance of keys in React lists?
- Keys help React identify which items have changed, are added, or are removed. They should be unique among siblings to ensure efficient updates.
- How do you handle forms in React?
- Forms can be handled using controlled components where form data is managed by the state within the component. Alternatively, uncontrolled components can be used with refs to access form data.
Practical Scenarios
- How would you optimize a slow React application?
- Use
React.memo
,useMemo
, anduseCallback
to prevent unnecessary re-renders. - Implement code splitting with
React.lazy
andSuspense
. - Optimize images and use lazy loading for heavy assets.
- Use performance profiling tools like React DevTools.
- Use
- How would you handle authentication in a React app?
- Use context or a state management library to store user authentication state.
- Protect routes using higher-order components or custom hooks.
- Store tokens securely (e.g., in HTTP-only cookies).
- How do you handle API calls in React?
- Use
useEffect
for side effects like fetching data. - Use libraries like
axios
orfetch
for making API calls. - Handle loading, error, and success states in the component.
- Use
- How would you implement pagination in React?
- Use state to track the current page and fetch data accordingly.
- Render pagination controls (e.g., buttons) to navigate between pages.
- Use a library like
react-paginate
for pre-built pagination components.
- How do you handle forms with validation in React?
- Use controlled components to manage form state.
- Implement validation logic using libraries like
Formik
orReact Hook Form
. - Display error messages based on validation rules.
React Ecosystem Questions
- What is Redux, and how does it work with React?
- Redux is a state management library that uses a single global store to manage application state. It works with React via the
react-redux
library.
- Redux is a state management library that uses a single global store to manage application state. It works with React via the
- What is the difference between Redux and Context API?
- Redux provides a centralized store with middleware and dev tools, while Context API is built into React and is simpler for smaller applications.
- What is Next.js, and how is it different from React?
- Next.js is a React framework that adds features like server-side rendering, static site generation, and API routes. React is a library for building UIs.
- What is React Router, and how do you use it?
- React Router is a library for routing in React applications. It allows you to define routes and navigate between components using
BrowserRouter
,Route
, andLink
.
- React Router is a library for routing in React applications. It allows you to define routes and navigate between components using
- What is the difference between
BrowserRouter
andHashRouter
?BrowserRouter
uses the HTML5 history API for clean URLs, whileHashRouter
uses the hash portion of the URL (e.g.,#/home
).
Behavioral and Problem-Solving Questions
- How do you debug a React application?
- Use React DevTools to inspect components and state.
- Add
console.log
statements or use breakpoints in the browser’s developer tools. - Check for errors in the browser console or network tab.
- How do you handle state management in a large React application?
- Use a state management library like Redux, Zustand, or Recoil.
- Organize state into smaller, manageable pieces.
- Use context for shared state that doesn’t require global management.
- What challenges have you faced while working with React, and how did you solve them?
- Example: Performance issues due to unnecessary re-renders. Solved by using
React.memo
anduseMemo
.
- Example: Performance issues due to unnecessary re-renders. Solved by using
- How do you stay updated with React and its ecosystem?
- Follow the official React blog, attend conferences, and participate in online communities like Reddit or Discord.
- How do you ensure code quality in a React project?
- Use ESLint and Prettier for code formatting and linting.
- Write unit and integration tests.
- Conduct code reviews and follow best practices.
These questions should give you a comprehensive understanding of React and prepare you for a wide range of interview scenarios.