React.
js Interview Questions and Answers
Q: What is React?
A: React is a JavaScript library for building user interfaces. It allows developers to create large web
applications that can update and render efficiently in response to data changes.
Q: What are the features of React?
A: JSX, Components, Virtual DOM, One-way data binding, Performance optimization, React Hooks.
Q: What is JSX?
A: JSX stands for JavaScript XML. It allows us to write HTML inside JavaScript and place them in
the DOM without using functions like createElement().
Q: What is the virtual DOM?
A: The virtual DOM is a programming concept where a virtual representation of the UI is kept in
memory and synced with the real DOM using libraries like ReactDOM.
Q: Difference between functional and class components?
A: Functional components are simpler, use hooks, and don't have lifecycle methods. Class
components use lifecycle methods and have more complex syntax.
Q: What is the difference between state and props?
A: Props are read-only and passed from parent to child. State is local and managed within the
component.
Q: What are lifecycle methods?
A: Lifecycle methods are special methods in class components that allow you to run code at
particular times in the component's lifecycle (e.g., componentDidMount).
Q: How to update state in React?
A: Use the setState() method in class components or the useState hook in functional components.
Q: What is lifting state up?
A: It's the process of moving state to a common ancestor of components that need to share it.
Q: What are React Hooks?
A: Hooks are functions that let you use state and other React features in functional components
(e.g., useState, useEffect).
Q: Explain useState.
A: useState is a Hook that lets you add React state to functional components. Example: const
[count, setCount] = useState(0);
Q: Explain useEffect.
A: useEffect lets you perform side effects in function components (like data fetching, DOM updates,
timers).
Q: Difference between useEffect and componentDidMount?
A: componentDidMount is used in class components. useEffect can replicate this behavior in
functional components with an empty dependency array.
Q: What is useRef?
A: useRef returns a mutable ref object which persists for the lifetime of the component. Useful for
accessing DOM elements directly.
Q: What is useContext?
A: useContext allows you to access context values without a Consumer component.
Q: What are custom hooks?
A: Custom hooks are functions that let you reuse logic across components.
Q: How do you render lists in React?
A: Use the map() function to loop through an array and return React elements.
Q: Why use a key prop?
A: Key helps React identify which items have changed, are added, or are removed.
Q: What is conditional rendering?
A: Rendering different UI elements based on conditions using if, ternary, or logical operators.
Q: How to handle forms in React?
A: Use controlled components with useState to handle form inputs.
Q: What are controlled vs uncontrolled components?
A: Controlled components have form data controlled by React state. Uncontrolled components rely
on the DOM.
Q: What is React Router?
A: A standard library for routing in React applications.
Q: How to implement routing?
A: Wrap components in <BrowserRouter>, and use <Route> and <Link> from react-router-dom.
Q: What is React.memo?
A: React.memo is a HOC that prevents unnecessary re-renders of functional components by
memoizing the result.
Q: What is lazy loading?
A: A technique to load components only when they're needed using React.lazy and Suspense.
Q: How to optimize performance?
A: Use memoization, code splitting, lazy loading, avoiding unnecessary renders.
Q: Difference between Redux and Context API?
A: Redux is a state management library. Context API is built into React and is good for simpler use
cases.
Q: What are higher-order components?
A: A function that takes a component and returns a new component with added features.
Q: What are pure components?
A: A component that does not re-render unless props or state change.
Q: What is reconciliation?
A: The process by which React updates the DOM by comparing the new and previous virtual DOM
trees.
Q: What is the significance of keys in React?
A: Keys help React identify elements in a list and track changes efficiently.