Home / Learn React / React Interview Questions and Answers
React Interview Questions and Answers
Short, clear answers to the questions asked most in React interviews, from basics to React 19.
Short answers you can say in an interview. Click a question to open the answer. Say each answer aloud in your own words.
Basic questions
What is React?
React is a JavaScript library for building user interfaces from small reusable pieces called components. It updates only the parts of the page that changed.
What is JSX?
JSX is a syntax that looks like HTML inside JavaScript. A build tool compiles it into function calls that create React elements.
What is the virtual DOM?
It is a lightweight copy of the UI that React keeps in memory. React compares the new tree with the old one (diffing) and updates only the real DOM nodes that changed.
What is the difference between props and state?
Props are inputs passed from the parent and are read-only. State is data owned by the component that can change and triggers a re-render.
Why do lists need a key?
Keys let React match items between renders, so it can keep the right state and move or remove the right DOM nodes. Use stable unique ids, not the index, when the list can change.
What is a controlled component?
An input whose value is stored in React state and updated by onChange, so state is the single source of truth.
What is the difference between a function and a class component?
Both return UI. Function components with hooks are the modern standard. Classes are still needed for error boundaries.
What are fragments?
A way to group several elements without adding an extra DOM node: <>...</>.
Hooks questions
Why can hooks not be called inside conditions?
React identifies hooks by the order of calls. A condition would change the order between renders and mix up their state.
What does useEffect do and when does it run?
It runs side effects after the component is drawn. With no dependency array it runs after every render, with [] once after mount and with values it runs when they change.
What is the cleanup function in useEffect?
The function returned from the effect. React calls it before re-running the effect and when the component unmounts, to remove timers, listeners or subscriptions.
What is the difference between useMemo and useCallback?
useMemo caches a calculated value. useCallback caches a function. Both only help when the result is passed to something that compares by reference or the calculation is expensive.
When do you use useRef?
To hold a mutable value that survives renders without causing a re-render, or to reach a DOM node, for example to focus an input.
useState or useReducer?
Use useState for simple values. Use useReducer when state has several related fields or many kinds of updates, so the logic is in one reducer.
Why does setCount(count + 1) twice only add one?
Both calls use the same count from the current render. Use the updater form setCount(c => c + 1) to chain updates.
What is a custom hook?
A function that starts with use and calls other hooks, so you can reuse stateful logic between components.
Advanced and React 19 questions
What is prop drilling and how do you avoid it?
Passing props through many levels that do not use them. Avoid it with composition, Context or a state library.
What problems does Context have?
Every consumer re-renders when the value changes. Split contexts, memoize the value or use a store such as Zustand for frequently changing data.
What are error boundaries?
Class components with getDerivedStateFromError that catch render errors in their children and show a fallback. They do not catch event handler or async errors.
What does Suspense do?
It shows a fallback while something below it, such as lazy code or a data promise, is not ready.
What is useActionState?
A React 19 hook for form Actions. It returns the last result of an action, a function to pass to <form action> and a pending flag.
What is useOptimistic?
A hook that shows a predicted state immediately while an async action runs, and rolls back to the real state when it ends.
What does the use API do?
It reads a promise (suspending until it resolves) or a context. Unlike hooks it can be called inside conditions.
What are Server Components?
Components that render only on the server and send no JavaScript to the browser. They need a framework. Client Components are marked with "use client".
What does React Compiler do?
It is a build time tool that adds memoization automatically, if your components follow the Rules of React.
How do you optimize a slow React app?
Measure with the Profiler. Then move state down, split components, virtualize long lists, lazy load code, and memoize only where measurements show it helps.
SQL and MySQL with React questions
Can React connect directly to MySQL?
No. React runs in the browser, so database credentials would be public. React calls an API (PHP, Node and so on) with fetch, and the server talks to MySQL.
What is SQL injection and how do you prevent it?
It is when user input becomes part of the SQL command. Prevent it with prepared statements (PDO placeholders), a whitelist for column names, input validation and a MySQL user with limited rights.
What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only rows that match in both tables. LEFT JOIN returns all rows from the left table and NULL for columns of the right table when nothing matches.
How do you implement pagination?
Send page and size to the API. The server runs LIMIT size OFFSET (page - 1) * size and a COUNT(*) for the total. React shows the rows and computes the page count.
Why is a primary key useful and why use it as the React key?
It uniquely identifies a row, so updates and deletes target the right record. As a React key it is stable, so React can match list items correctly between renders.
How do you store passwords?
Never in plain text. Use password_hash() in PHP (bcrypt or argon2, salted and slow) and password_verify() to check a login.
How do you keep a user logged in with PHP and React?
PHP stores the user id in a server session and sends an HttpOnly cookie. React calls fetch with credentials same-origin and asks a "me" endpoint on load to learn who is logged in.
What is an index and when can it hurt?
An index is a sorted structure that speeds up WHERE, JOIN and ORDER BY. Too many indexes slow down INSERT, UPDATE and DELETE and use disk space.
What is a transaction?
A group of statements that succeed or fail together (ACID). Use START TRANSACTION, COMMIT and ROLLBACK, for example when moving money between two accounts.
Why validate again on the server when React already validates?
Anyone can call the API directly and skip the React form. Only the server check protects the data.