React
30 Questions
Practice Problems
React Interview Questions
30 Questions
React Basics
What is React?
React is a JavaScript library used for building fast and interactive user interfaces, mainly for single-page applications. It was developed by Meta and follows a component-based architecture, making code reusable, organized, and easier to maintain in modern frontend development. Example: function App() { return <h1>Hello React</h1>; }
What are the features of React?
React provides reusable components, Virtual DOM, one-way data binding, hooks, and fast rendering. It improves application performance and simplifies UI development. React also supports strong community libraries, making it suitable for scalable and modern frontend applications. Example: <Component />
What is JSX in React?
JSX stands for JavaScript XML. It allows developers to write HTML-like syntax inside JavaScript code. JSX makes React components easier to read and write. Browsers cannot understand JSX directly, so it gets converted into regular JavaScript using transpilers like Babel. Example: const element = <h1>Hello</h1>;
What is the Virtual DOM?
The Virtual DOM is a lightweight copy of the real DOM maintained by React. When data changes, React updates the Virtual DOM first, compares differences, and updates only changed elements in the real DOM. This process improves rendering performance significantly. Example: setCount(count + 1);
How does React work?
React creates UI using components and updates the interface whenever data changes. It uses the Virtual DOM and reconciliation process to efficiently update only modified parts of the webpage instead of reloading the entire page, improving speed and user experience. Example: root.render(<App />);
What are React components?
React components are reusable building blocks used to create user interfaces. Each component manages its own structure, logic, and styling. Components help organize applications into smaller parts, making development, testing, and maintenance easier in large projects. Example: function Header() { return <h1>Header</h1>; }
What is the difference between functional and class components?
Functional components are simple JavaScript functions and mainly use hooks for state management. Class components use ES6 classes and lifecycle methods. Modern React development mostly prefers functional components because they are cleaner, shorter, and easier to maintain. Example: function App() {} class App extends React.Component {}
What are props in React?
Props, short for properties, are used to pass data from parent components to child components. They are read-only and help make components reusable. Props allow dynamic rendering based on different values received from parent components. Example: <Greeting name="Sam" />
What is state in React?
State is an object used to store dynamic data inside a component. When state changes, React automatically re-renders the component. State helps manage user interactions, form inputs, API responses, and other changing data within applications. Example: const [count, setCount] = useState(0);
What is the difference between props and state?
Props are passed from parent to child components and cannot be modified by the receiving component. State belongs to the component itself and can change over time. Props handle external data, while state manages internal dynamic data. Example: props.name state.count
React Hooks
What are Hooks in React?
Hooks are special functions introduced in React that allow functional components to use state and lifecycle features. Hooks simplify component logic and reduce the need for class components. Common hooks include useState, useEffect, and useContext. Example: useState();
What is useState()?
useState() is a React hook used to create and manage state inside functional components. It returns the current state value and a function to update it. Updating the state automatically re-renders the component with new data. Example: const [name, setName] = useState("");
What is useEffect()?
useEffect() is a React hook used for handling side effects such as API calls, timers, and DOM updates. It runs after component rendering and can execute whenever dependencies change, helping manage asynchronous and lifecycle-related operations. Example: useEffect(() => { console.log("Rendered"); }, []);
What is the dependency array in useEffect()?
The dependency array controls when useEffect() executes. If empty, it runs only once after initial render. If dependencies are included, it runs whenever those values change. Without the array, the effect runs after every component render. Example: useEffect(() => {}, [count]);
What is useContext()?
useContext() is a React hook used to access shared data from Context API without passing props manually through multiple components. It helps avoid prop drilling and makes global state management simpler in medium-sized applications. Example: const value = useContext(UserContext);
What is useRef()?
useRef() creates a mutable reference object that persists across renders without causing re-rendering. It is commonly used to access DOM elements directly, store previous values, or manage timers and input focus inside components. Example: const inputRef = useRef();
What is useMemo()?
useMemo() is a React hook used to memoize expensive calculations. It prevents unnecessary recalculations by storing computed values unless dependencies change. This improves application performance, especially when rendering large lists or complex computations. Example: const result = useMemo(() => total(), [items]);
What is useCallback()?
useCallback() memoizes functions so they are not recreated on every render unless dependencies change. It helps optimize performance when passing functions to child components, preventing unnecessary re-renders in React applications. Example: const handleClick = useCallback(() => {}, []);
What is the difference between useMemo and useCallback?
useMemo() memoizes computed values, while useCallback() memoizes functions. Both improve performance by avoiding unnecessary recalculations or recreations. useMemo returns a value, whereas useCallback returns a memoized function. Example: useMemo(() => value, []); useCallback(() => func(), []);
What are custom hooks in React?
Custom hooks are reusable JavaScript functions that use React hooks internally. They help share logic between components without duplicating code. Custom hooks usually start with the word use and improve maintainability in large applications. Example: function useFetch() {}
Component Communication & Rendering
What is prop drilling?
Prop drilling happens when data is passed through multiple intermediate components just to reach a deeply nested child component. It makes code difficult to maintain and increases complexity in large React applications. Example: <App user={user} />
How do you avoid prop drilling?
Prop drilling can be avoided using Context API, Redux, Zustand, or other state management libraries. These solutions allow components to access shared data directly without passing props through multiple component levels. Example: <UserContext.Provider value={user}>
What is lifting state up?
Lifting state up means moving shared state to the nearest common parent component so multiple child components can access and update the same data. This improves synchronization and data sharing between related components. Example: const [value, setValue] = useState("");
What is conditional rendering?
Conditional rendering means displaying different UI elements based on conditions or state values. React commonly uses if, ternary operators, or logical operators to dynamically show or hide content according to application logic. Example: {isLoggedIn ? <Home /> : <Login />}
What causes re-rendering in React?
React components re-render when state changes, props change, parent components re-render, or context values update. Re-rendering helps update the UI with new data, but unnecessary renders can affect application performance. Example: setCount(count + 1);
How do you optimize React performance?
React performance can be optimized using memoization, lazy loading, code splitting, React.memo, useMemo, useCallback, and efficient state management. Avoiding unnecessary re-renders and optimizing large lists also improves application speed significantly. Example: export default React.memo(Component);
Routing, API & Advanced Topics
What is React Router?
React Router is a library used for navigation in React applications. It enables routing between pages without refreshing the browser. Developers use components like BrowserRouter, Routes, and Route to create single-page application navigation. Example: <Route path="/about" element={<About />} />
How do you fetch API data in React?
API data is commonly fetched in React using fetch() or Axios inside useEffect(). After receiving the response, developers store the data in state and render it dynamically inside components. Example: useEffect(() => { fetch(url); }, []);
What is Context API?
Context API is React's built-in state management system used to share data globally between components. It eliminates unnecessary prop passing and is commonly used for themes, authentication, and user-related information. Example: const UserContext = createContext();
What is the difference between Context API and Redux?
Context API is suitable for simple global state management, while Redux is designed for complex applications with predictable state updates. Redux provides middleware, centralized state handling, and advanced debugging tools, making it better for large-scale applications. Example: const store = configureStore({});