Q31React Hooks
What are the rules of hooks?
Call hooks only at the top level of function components or custom hooks, never conditionally or inside loops.
Answer
Call hooks only at the top level of function components or custom hooks, never conditionally or inside loops.
Key Points
- Hook order must remain stable
- Use lint rules
Q32React Hooks
How does the useEffect dependency array work?
It tells React which reactive values an effect uses to determine when to re-synchronize.
Answer
It tells React which reactive values an effect uses to determine when to re-synchronize.
Key Points
- Include reactive values used by the effect
- Do not suppress warnings casually
Q33React Hooks
Why does an effect need cleanup?
Cleanup releases external resources such as subscriptions, timers, listeners, or in-flight work.
Answer
Cleanup releases external resources such as subscriptions, timers, listeners, or in-flight work.
Key Points
- Runs before re-synchronization when needed
- Runs on unmount
Q34React Data
How do you prevent stale network responses from overwriting newer results?
Cancel obsolete requests with AbortController or track request identity so only the latest relevant response updates state.
Answer
Cancel obsolete requests with AbortController or track request identity so only the latest relevant response updates state.
Key Points
- Handle aborts separately
- Clean up when query changes
Q35React Hooks
What is useMemo?
useMemo caches a calculated value between renders until dependencies change, potentially avoiding expensive recalculation.
Answer
useMemo caches a calculated value between renders until dependencies change, potentially avoiding expensive recalculation.
Key Points
- Optimization, not correctness guarantee
- Dependencies must be accurate
Q36React Hooks
What is useCallback?
useCallback caches a function definition until its dependencies change.
Answer
useCallback caches a function definition until its dependencies change.
Key Points
- Useful when function identity matters
- Do not apply everywhere automatically
Q37React Performance
What does React.memo do?
React.memo can skip rendering a component when its props are unchanged according to its comparison behavior.
Answer
React.memo can skip rendering a component when its props are unchanged according to its comparison behavior.
Key Points
- New object props can defeat shallow comparison
- State and context can still trigger renders
Q38React Performance
How do you optimize a slow React component?
Profile first, then target expensive calculations, unnecessary renders, large lists, or poorly placed state.
Answer
Profile first, then target expensive calculations, unnecessary renders, large lists, or poorly placed state.
Key Points
- Measure before and after
- Check network and browser costs too
Q39React Performance
What is list virtualization?
Virtualization renders only visible rows and a small buffer, reducing DOM nodes for large lists.
Answer
Virtualization renders only visible rows and a small buffer, reducing DOM nodes for large lists.
Key Points
- Useful for very large collections
- Preserve accessibility
Q40React State
When should you use Context API?
Context shares values needed by many descendants, such as theme, locale, or current user, without passing props through every layer.
Answer
Context shares values needed by many descendants, such as theme, locale, or current user, without passing props through every layer.
Key Points
- Split contexts by update pattern
- Context updates can re-render consumers
Q41React State
When use a state management library?
Use one when shared client state has complex updates, debugging needs, or coordination across many components.
Answer
Use one when shared client state has complex updates, debugging needs, or coordination across many components.
Key Points
- Consider local state first
- Distinguish server state from client state
Q42React Hooks
What is useReducer?
useReducer manages state through a reducer that receives current state and an action and returns next state.
Answer
useReducer manages state through a reducer that receives current state and an action and returns next state.
Key Points
- Reducers should be pure
- Useful for related transitions
Q43React with TypeScript
How do you type a reducer?
Define the state type and a discriminated union for actions, then type reducer arguments and return value.
Answer
Define the state type and a discriminated union for actions, then type reducer arguments and return value.
Key Points
- Use exhaustive switches
- Avoid any
Q44React with TypeScript
How do you type a DOM useRef?
Use a generic such as useRef<HTMLInputElement | null>(null) for a DOM ref initialized to null.
Answer
Use a generic such as useRef<HTMLInputElement | null>(null) for a DOM ref initialized to null.
Key Points
- Check null before access
- Attach it to a compatible element
Q45React with TypeScript
How do you type a callback prop?
Declare its function signature, such as onSelect: (id: string) => void.
Answer
Declare its function signature, such as onSelect: (id: string) => void.
Key Points
- Use precise parameter types
- Keep callback contracts clear
Q46React with TypeScript
How do you type style and className props?
Use React.CSSProperties for inline styles and string for className, or extend native HTML attribute types when wrapping elements.
Answer
Use React.CSSProperties for inline styles and string for className, or extend native HTML attribute types when wrapping elements.
Key Points
- Prefer CSS classes for most styling
- Avoid any
Q47React Forms
How do you validate a React form?
Validate values, show accessible field errors, manage submission states, and validate again on the server.
Answer
Validate values, show accessible field errors, manage submission states, and validate again on the server.
Key Points
- Client validation is not a security boundary
- Associate errors with fields
Q48React Forms
onChange versus onSubmit?
onChange responds to input changes; onSubmit handles form submission, including Enter-key submission.
Answer
onChange responds to input changes; onSubmit handles form submission, including Enter-key submission.
Key Points
- Use semantic form elements
- Prevent default navigation when needed
Q49React Routing
What is client-side routing?
Client-side routing updates the displayed view based on the URL without a full document reload for every navigation.
Answer
Client-side routing updates the displayed view based on the URL without a full document reload for every navigation.
Key Points
- Support back and forward
- Handle direct URL loads
Q50React Data
How do you fetch data in React?
Use fetch or a data library and explicitly handle loading, success, errors, cancellation, and caching.
Answer
Use fetch or a data library and explicitly handle loading, success, errors, cancellation, and caching.
Key Points
- Do not fetch during ordinary render
- Use framework data APIs where appropriate
Q51React Data
Server state versus client state?
Server state is remotely owned and needs synchronization and caching; client state is owned locally by the UI or application.
Answer
Server state is remotely owned and needs synchronization and caching; client state is owned locally by the UI or application.
Key Points
- API records are server state
- Open modal state is client state
Q52React Testing
What is React Testing Library?
It provides utilities for testing components through user-visible behavior and accessible queries.
Answer
It provides utilities for testing components through user-visible behavior and accessible queries.
Key Points
- Prefer role and label queries
- Test interactions and outcomes
Q53React Testing
How do you test asynchronous UI?
Wait for observable UI changes and test loading, success, and error states with controlled promises or network mocks.
Answer
Wait for observable UI changes and test loading, success, and error states with controlled promises or network mocks.
Key Points
- Use findBy or waitFor appropriately
- Avoid arbitrary sleeps
Q54React Accessibility
How do you make React components accessible?
Use semantic HTML, labels, keyboard support, visible focus, appropriate ARIA, and accessible error messages.
Answer
Use semantic HTML, labels, keyboard support, visible focus, appropriate ARIA, and accessible error messages.
Key Points
- Prefer native controls
- Test keyboard navigation
Q55React Error Handling
What is an error boundary?
An error boundary catches certain errors during descendant rendering and can show fallback UI, but does not catch every async or event-handler erro...
Answer
An error boundary catches certain errors during descendant rendering and can show fallback UI, but does not catch every async or event-handler error.
Key Points
- Can be implemented with a class or library
- Provide recovery where possible
Q56React Performance
What is code splitting?
Code splitting divides app code into smaller bundles that can load when needed, often through dynamic imports and lazy loading.
Answer
Code splitting divides app code into smaller bundles that can load when needed, often through dynamic imports and lazy loading.
Key Points
- Reduces initial JavaScript
- Provide useful loading fallbacks
Q57React Fundamentals
What is reconciliation?
Reconciliation compares previous and next element descriptions to determine necessary UI updates.
Answer
Reconciliation compares previous and next element descriptions to determine necessary UI updates.
Key Points
- Type and keys affect identity
- Use stable keys
Q58React Hooks
Why might an effect run too often?
Dependencies may change every render, for example newly created objects or functions, or dependencies may not match the intended synchronization.
Answer
Dependencies may change every render, for example newly created objects or functions, or dependencies may not match the intended synchronization.
Key Points
- Move calculations when appropriate
- Keep real dependencies
Q59React State
Why treat state as immutable?
Immutable updates create new references and make changes easier to reason about, avoiding shared mutation bugs.
Answer
Immutable updates create new references and make changes easier to reason about, avoiding shared mutation bugs.
Key Points
- Use map, filter, and spread appropriately
- Update nested data deliberately
Q60React with TypeScript
What is a discriminated union?
It models related shapes with a shared literal property so TypeScript can narrow types safely in branches and reducers.
Answer
It models related shapes with a shared literal property so TypeScript can narrow types safely in branches and reducers.
Key Points
- Useful for async states
- Use exhaustive checks
Q61React Hooks
useLayoutEffect versus useEffect?
useLayoutEffect runs after DOM mutations but before repaint, useful for layout measurement without flicker; useEffect is generally preferred for ot...
Answer
useLayoutEffect runs after DOM mutations but before repaint, useful for layout measurement without flicker; useEffect is generally preferred for other synchronization.
Key Points
- Layout effects can block paint
- Use sparingly
Q62React Fundamentals
What is hydration?
Hydration attaches React behavior to server-rendered HTML so the existing markup becomes interactive.
Answer
Hydration attaches React behavior to server-rendered HTML so the existing markup becomes interactive.
Key Points
- Initial output should match
- Browser-only values can cause mismatches
Q63React Fundamentals
What causes hydration mismatch?
It occurs when server HTML differs from the client's initial expected render.
Answer
It occurs when server HTML differs from the client's initial expected render.
Key Points
- Avoid random initial output
- Check dates, locale, browser APIs, and invalid HTML
Q64React Data
What is optimistic UI?
Optimistic UI updates the interface before the server confirms a mutation, then reconciles or rolls back if it fails.
Answer
Optimistic UI updates the interface before the server confirms a mutation, then reconciles or rolls back if it fails.
Key Points
- Provide error recovery
- Best for predictable actions
Q65React Performance
What is Suspense?
Suspense displays a fallback while supported child operations, such as lazy code loading or framework-integrated data, are pending.
Answer
Suspense displays a fallback while supported child operations, such as lazy code loading or framework-integrated data, are pending.
Key Points
- Only supported sources suspend
- Error boundaries handle errors separately
Q66React Architecture
How do you decide where state should live?
Place state at the lowest common owner that needs it, sharing only when multiple consumers need coordination.
Answer
Place state at the lowest common owner that needs it, sharing only when multiple consumers need coordination.
Key Points
- Keep transient UI state local
- Separate server and client state
Q67React with TypeScript
How do you type limited component variants?
Use a string literal union such as 'primary' | 'secondary' | 'danger' so invalid values are caught by TypeScript.
Answer
Use a string literal union such as 'primary' | 'secondary' | 'danger' so invalid values are caught by TypeScript.
Key Points
- Use exhaustive maps or switches
- Avoid broad string types
Q68React Architecture
What are compound components?
Related components coordinate through composition or context to provide a flexible declarative API, such as Tabs and TabPanel.
Answer
Related components coordinate through composition or context to provide a flexible declarative API, such as Tabs and TabPanel.
Key Points
- Define accessibility clearly
- Avoid hidden coupling
Q69React Architecture
What is the render props pattern?
A component receives a function prop that returns UI, letting consumers control rendering while sharing behavior.
Answer
A component receives a function prop that returns UI, letting consumers control rendering while sharing behavior.
Key Points
- Hooks often replace this pattern
- Function identity may matter
Q70React Architecture
What is a higher-order component?
A higher-order component is a function that takes a component and returns an enhanced component.
Answer
A higher-order component is a function that takes a component and returns an enhanced component.
Key Points
- Common in older code
- Hooks and composition may be simpler