React State (useState & Local State Basics) — Complete Theory Guide
1. Introduction
What is State?
State in React represents data that changes over time and directly influences what gets rendered on the UI.- It is mutable (changeable) data managed within a component
- When state changes → React re-renders the component
useState hook to manage local state in functional components.
Why is State Important in React?
React is built around declarative UI:UI = f(state)That means:
- UI automatically reflects the current state
- You don’t manually update the DOM
- UI would be static
- No interactivity (forms, buttons, toggles, etc.)
When and Why We Use State
Use state when:- Data changes over time
- UI needs to respond to user actions
- You need to store temporary UI data
- Form inputs
- Toggle (show/hide)
- Counters
- API response data
- UI states (loading, error, success)
2. Concepts / Internal Workings
2.1 Local State vs Global State
-
Local State
- Managed inside a component
- Not shared unless passed via props
-
Global State
- Shared across components
- Managed via Context, Redux, etc.
2.2 How useState Works Internally
React uses an internal mechanism called Hooks Dispatcher + Fiber Tree.
Key ideas:
- React stores state outside the component function
-
Each render:
- React maps hooks by call order
- Not by variable name
Hooks must always be called in the same order
2.3 State Update Process
- You call
setState - React schedules an update
- Component re-renders
- New state value is used
- React may batch updates
- Updates are asynchronous
2.4 Functional Updates
When next state depends on previous state:- Prevents stale state issues
- Works correctly with batching
2.5 Re-rendering Behavior
- State change → triggers re-render
- Entire component function runs again
- React compares virtual DOM → updates only necessary parts
2.6 Relationship with Other React Features
With Props
- State is internal
- Props are external
- State can be passed down as props
With useEffect
- State changes can trigger side effects
With Rendering
- State directly controls UI output
3. Syntax & Examples
3.1 Basic useState Example
3.2 Multiple State Variables
3.3 Object State
3.4 Array State
3.5 Toggle Example
3.6 Controlled Input
3.7 Lazy Initialization
- Runs only once on initial render
3.8 Derived State (Avoid storing when possible)
❌ Bad:4. Edge Cases / Common Mistakes
4.1 State Updates Are Asynchronous
- Use functional update or
useEffect
4.2 Stale State Problem
4.3 Mutating State Directly
❌ Wrong:4.4 Missing Dependencies in Effects
4.5 Too Much State
- Storing unnecessary data
- Leads to complexity and bugs
4.6 Using State for Derived Values
Avoid storing values you can compute4.7 Infinite Re-renders
- Move into event or effect
5. Best Practices
5.1 Keep State Minimal
Store only:- What changes
- What is necessary
5.2 Use Functional Updates When Needed
Always use when:- Next state depends on previous
5.3 Normalize State Shape
Avoid deeply nested objects: ❌ Bad:5.4 Split State Logically
Instead of:5.5 Avoid Unnecessary Re-renders
- Use memoization (
React.memo,useMemo) - Avoid updating state if value hasn’t changed
5.6 Use Lazy Initialization for Expensive Work
5.7 Naming Conventions
-
Use clear naming:
count / setCountisOpen / setIsOpen
5.8 Keep State Close to Usage
- Lift state only when necessary
- Avoid over-sharing state
5.9 Prefer Derived Data Over Stored Data
total
5.10 Think in UI State, Not Variables
Ask:“What UI behavior does this state represent?”
Final Summary
- State is the core of interactivity in React
-
useStateenables functional components to manage dynamic data -
Understanding:
- Re-rendering
- Asynchronous updates
- State immutability
- Hooks
- Performance optimization
- Scalable React architecture
Senior-Level Conceptual Questions — React State (useState & Local State)
1. Why does React rely on call order to track useState instead of variable names?
Answer:
React identifies hooks using call order, not variable names, because:
- Components re-run on every render → variables are re-created
- React needs a stable mapping between renders
- Call order is deterministic as long as rules are followed
- React stores hooks in a linked list (Fiber node)
- Each
useStatecall reads the next slot
- Faster than name-based tracking
- Avoids runtime parsing or identifiers
- Key-based hooks → slower, more complex
2. Why are state updates asynchronous and batched?
Answer: React batches updates for:-
Performance
- Multiple updates → single re-render
-
Consistency
- Avoid intermediate UI states
- Updates go into a queue
- React schedules rendering via Fiber
- You can’t rely on immediate state
- Functional updates
useEffectfor post-update logic
3. What problem does the functional update form solve?
Answer: It solves stale closure issues.- Closures capture old values
- Multiple updates may use outdated
count
- Functional updates read from React’s internal queue, not closure
4. Why does mutating state directly fail to trigger re-renders?
Answer: React relies on reference comparison, not deep checks.- Deep comparison is expensive
- Immutability enables fast diffing
5. How does React decide whether to re-render a component after state change?
Answer: React always re-renders the component when state updates, but:- It diffs virtual DOM
- Only updates changed parts
React.memouseMemouseCallback
- Re-render ≠ DOM update
6. What are the trade-offs of storing derived state?
Answer: Derived state = computed from other state ❌ Bad:- Duplication
- Sync bugs
- Extra renders
- Expensive computation
- Memoized with
useMemo
7. Why is useState not suitable for complex state logic?
Answer:
useState works best for simple, isolated values
Problems with complexity:
- Multiple interdependent states
- Hard to manage updates
- Logic duplication
useReducer
- Centralized logic
- Predictable transitions
8. What happens internally when setState is called?
Answer:
- Create an update object
- Push into update queue
- Schedule render (Fiber scheduler)
-
During render:
- Apply queued updates
- Compute new state
- Not applied immediately
- Happens in next render cycle
9. Why can calling setState in render cause infinite loops?
Answer:
Render → setState → render → setState → loop
- Move to event or
useEffect
10. How does React handle multiple useState calls in one component?
Answer:
Each call:
- Gets its own slot in hook list
- Independent state
- Avoid unnecessary re-renders
- Better separation of concerns
11. When should you lift state up vs keep it local?
Answer: Keep local if:- Used by one component
- Shared across components
- Lifting increases coupling
- Local improves encapsulation
12. Why is lazy initialization useful in useState?
Answer:
- Runs only on first render
- Avoids repeated computation
- Component function runs on every render
13. What are stale closures and how do they affect state?
Answer: Closures capture old state values14. Why should state be minimal and normalized?
Answer: Problems with large/nested state:- Hard updates
- Bugs
- Performance issues
- Flat structure
- Easier updates
15. How does state affect component performance?
Answer: Each state update:- Triggers re-render
- Frequent updates
- Large component trees
- Split components
- Memoization
- Avoid unnecessary state
16. Why is it dangerous to rely on state immediately after setting it?
Answer: Because updates are queued- Use
useEffect - Or functional updates
17. How do you prevent unnecessary state updates?
Answer: Check before updating:- If same value → no re-render
18. What’s the difference between state and refs for storing values?
Answer:| Feature | State | Ref |
|---|---|---|
| Causes re-render | ✅ | ❌ |
| Persistent value | ✅ | ✅ |
| UI-driven | ✅ | ❌ |
- Mutable values
- DOM access
- Avoid re-renders
19. Why does React recommend splitting state instead of one big object?
Answer:- Partial updates tricky
- Unnecessary re-renders
- Fine-grained updates
20. How does local state impact scalability in large applications?
Answer: Local state:- Great for encapsulation
- Reduces global complexity
- Hard to share
- Leads to prop drilling
-
Combine with:
- Context
- State libraries
Final Insight
Senior engineers don’t just “use state” — they:- Understand how React schedules updates
- Design minimal, predictable state
- Avoid derived and duplicated data
- Optimize for render performance and scalability
Senior-Level MCQs — React State (useState & Local State)
1. What will be logged?
countinside the function is stale (0)- Both
setCount(count + 1)use the same value → React batches → final state = 1 - But
console.log(count)runs before re-render, so logs0
- B: Final state is 1, but not logged here
- C: Requires functional updates
- D: Batching doesn’t affect the logged value here
2. Which scenario guarantees correct increment behavior?
Options: A.- Functional updates always use latest state from React queue
- A: Stale closure → only +1
- C: Still captures stale
count - D: Mutates variable, breaks React model
3. Why does this NOT re-render?
- React uses shallow comparison
- Same object reference → no re-render
- A: React supports objects
- B: State can be any type
- D: Async is unrelated here
4. What happens if hooks are conditionally called?
- React relies on consistent hook order
- Conditional hooks break mapping → unpredictable state
- A: Not allowed
- B: Always unsafe, not just strict mode
- D: Not necessarily looping
5. What will this render?
- Initial render → 0
- Effect runs → updates to 1 → re-render
- A: misses update
- B: ignores first render
- D: dependency array prevents loop
6. Why is this problematic?
totalcan be derived fromitems- Risk of inconsistency
- A: trivial computation
- C: numbers are valid
- D: irrelevant
7. What will happen?
- React batches updates
- Same value → only one render
- A: batching avoids this
- C: state change still occurs
- D: valid code
8. What is the issue here?
datachanges → effect runs → updatesdata→ loop
- A: dependency is present (problem is using it)
- C: not required
- D: arrays are valid
9. Which is the safest way to update nested state?
Options: A.- Preserves immutability at all levels
- A/D: mutation
- B: overwrites entire state
10. What does lazy initialization prevent?
- Function runs only once (initial render)
11. What happens here?
- Closure captures old
count
12. Which is true about state vs ref?
Options: A. Both trigger re-render B. Only ref triggers re-render C. Only state triggers re-render D. Neither triggers re-render Correct Answer: C13. Why split state instead of one object?
Options: A. Required by React B. Better performance and isolation C. Objects are slower D. Hooks limitation Correct Answer: B14. What happens if same state value is set?
15. Why avoid setting state inside render?
Options: A. Performance only B. Infinite loop risk C. Not allowed syntax D. Only for class components Correct Answer: B16. What is true about batching?
Options: A. Only in events B. Only in async code C. Happens automatically in React D. Not predictable Correct Answer: C17. What is the main issue here?
- State initializes once
- Does not sync with props automatically
18. What is the best alternative?
Options: A. Always use state B. Derive directly from props C. Use global state D. Use ref Correct Answer: BFinal Takeaway
These questions test whether you understand:- State is queued, not immediate
- Closures + batching = tricky bugs
- Immutability drives re-rendering
- Design decisions matter more than syntax
React Coding Problems — State (useState & Local State Basics)
These problems simulate real-world product scenarios and are designed to test deep understanding of state behavior, edge cases, and design decisions.
1. Debounced Search Input
Problem
Build a search input that:- Updates UI immediately as user types
- Calls API only after user stops typing for 500ms
Constraints
- Avoid unnecessary API calls
- Must handle rapid typing
- No external debounce libraries
Expected Behavior
- Typing “react” quickly → only 1 API call
- UI still reflects typed value instantly
Edge Cases
- Fast typing
- Clearing input quickly
- Component unmount before timeout
Solution Explanation
Key idea:- Store input state
- Use timeout (debounce logic)
- Separates UI state vs API state
- Prevents excessive renders/API calls
2. Multi-Step Form with Validation
Problem
Create a 3-step form:- Each step has inputs
- State persists across steps
- Validation per step
Constraints
- Cannot lose data on navigation
- Must prevent moving forward on invalid input
Expected Behavior
- Step navigation with preserved state
- Validation errors shown per step
Edge Cases
- Jumping back and forth
- Partial completion
- Reset form
Solution Explanation
Use centralized state:- Avoid splitting state across steps
- Easier validation + persistence
3. Undo/Redo Feature
Problem
Implement undo/redo for a text editor.Constraints
- Maintain history
- Efficient updates
Expected Behavior
- Undo goes to previous state
- Redo restores undone state
Edge Cases
- Multiple undo levels
- Undo after new change (clear redo stack)
Solution Explanation
- Avoid mutation
- Maintain linear history
4. Toggle with Delayed Reset
Problem
Toggle a flag ON, but auto-reset after 3 seconds.Constraints
- Cancel previous timers on re-toggle
Expected Behavior
- Clicking rapidly resets timer
Edge Cases
- Multiple clicks
- Unmount during timer
Solution Explanation
Use cleanup:- Prevent stale timers
5. Derived State Trap
Problem
Show cart total from items list.Constraint
- Items can change dynamically
Expected Behavior
- Total always accurate
Edge Case
- Removing items
Solution Explanation
❌ Avoid:- Prevent inconsistency
6. Batched Updates Bug
Problem
Fix incorrect counter increment behavior.Bug Code
Expected Behavior
+2 incrementSolution
- Avoid stale closure
7. Sync State with Props
Problem
Component receivesvalue prop but allows editing locally.
Constraint
- Must update when prop changes
Solution
- Sync external changes
8. Dynamic Form Fields
Problem
Add/remove input fields dynamically.Expected Behavior
- Each field has its own state
- Add/remove works seamlessly
Solution
- Array state management
9. Prevent Unnecessary Re-renders
Problem
Avoid re-render when setting same value.Solution
WHY
- React may skip, but explicit check helps logic clarity
10. Loading State Race Condition
Problem
Multiple API calls → wrong loading stateSolution
Track request id:- Prevent stale responses
11. Controlled vs Uncontrolled Input Issue
Problem
Input loses value on re-renderSolution
- Controlled state ensures consistency
12. Infinite Loop with State
Problem
Solution
- Fix logic or remove dependency
- State update triggers effect repeatedly
13. Optimistic UI Update
Problem
Update UI before API responseSolution
- Improves UX
14. Toggle List Item Selection
Problem
Select/unselect items in listSolution
15. Persist State in LocalStorage
Problem
Keep state after refreshSolution
16. Avoid State Explosion
Problem
Too many related statesSolution
Group logically OR use reducer WHY:- Maintainability
17. Modal State Management
Problem
Multiple modals with independent controlSolution
- Single source of truth
18. Form Reset After Submit
Problem
Reset form correctlySolution
Edge Case
- Partial reset
19. Conditional Rendering Bug
Problem
Issue
0hides component
Fix
20. State Co-location vs Lifting
Problem
Where to place state?Solution
- Keep local if possible
- Lift only when shared
- Avoid unnecessary complexity
Final Insight
These problems evaluate:- State correctness under async conditions
- Understanding of React internals
- Design trade-offs (local vs global)
- Real-world bug handling
React Debugging Challenges — State (useState & Local State Basics)
These simulate real production bugs you’d catch in code reviews at senior level.
1. Stale Closure in Event Handler
Buggy Code
What’s Wrong?
- Uses stale
countinside closure
WHY It Happens
setTimeoutcaptures value at render time- React state updates don’t mutate existing closures
Fix
Best Practice
- Always use functional updates for async logic
2. State Mutation Causing No Re-render
Buggy Code
What’s Wrong?
- Direct mutation → no re-render
WHY
- React compares reference
- Same object → no update
Fix
Best Practice
- Treat state as immutable
3. Double Increment Bug
Buggy Code
What’s Wrong?
- Only increments once
WHY
- Both use same stale value
- React batches updates
Fix
Best Practice
- Use functional updates when depending on previous state
4. Infinite Re-render Loop
Buggy Code
What’s Wrong?
- Infinite loop
WHY
- State update triggers effect → loop
Fix
Best Practice
- Carefully design dependencies
5. Derived State Desync
Buggy Code
What’s Wrong?
totalnot updated whenitemschanges
WHY
- Missing dependency
Fix
Best Practice
- Avoid derived state
6. Uncontrolled → Controlled Warning
Buggy Code
What’s Wrong?
- Starts uncontrolled → becomes controlled
WHY
- Initial
undefined
Fix
Best Practice
- Initialize controlled inputs properly
7. Lost State on Re-render
Buggy Code
What’s Wrong?
- State update during render
WHY
- Triggers infinite loop risk
Fix
Best Practice
- Never update state inside render
8. Incorrect State Sync with Props
Buggy Code
What’s Wrong?
- Doesn’t update when prop changes
WHY
useStateonly initializes once
Fix
Best Practice
- Avoid copying props unless necessary
9. Race Condition in API Calls
Buggy Code
What’s Wrong?
- Old responses overwrite new ones
WHY
- Async order not guaranteed
Fix
Best Practice
- Handle async race conditions
10. Memory Leak with Timeout
Buggy Code
What’s Wrong?
- Timeout not cleared
WHY
- Runs after unmount
Fix
Best Practice
- Always cleanup side effects
11. Excessive Re-renders from State
Buggy Code
What’s Wrong?
- Re-render on every keystroke
WHY
- State updates trigger render
Fix (if heavy UI)
Best Practice
- Optimize frequent updates
12. Wrong Dependency Causing Loop
Buggy Code
What’s Wrong?
- Infinite loop
WHY
- Effect depends on value it updates
Fix
Best Practice
- Dependencies should reflect inputs, not outputs
13. State Reset on Key Change
Buggy Code
What’s Wrong?
- State resets unexpectedly
WHY
- Changing key remounts component
Fix
- Avoid unnecessary key changes
Best Practice
- Use stable keys
14. Overwriting State Instead of Merging
Buggy Code
What’s Wrong?
- Other fields lost
WHY
useStatereplaces, not merges
Fix
Best Practice
- Always merge manually
15. Incorrect Toggle Logic
Buggy Code
What’s Wrong?
- Can break with async updates
WHY
- Uses stale value
Fix
Best Practice
- Always use functional toggle
16. Expensive Initialization on Every Render
Buggy Code
What’s Wrong?
- Runs on every render
WHY
- Function executed immediately
Fix
Best Practice
- Use lazy initialization
17. Improper List Update
Buggy Code
What’s Wrong?
- Mutation
WHY
- Same reference
Fix
Best Practice
- Immutable updates
18. Conditional Hook Call
Buggy Code
What’s Wrong?
- Breaks hook order
WHY
- React relies on call order
Fix
- Move hook outside condition
Best Practice
- Follow Rules of Hooks strictly
Final Insight
These debugging scenarios reflect real production issues:- Closures + async = subtle bugs
- Immutability = core to React rendering
- Effects + dependencies = major failure point
- State design = architecture decision, not just syntax
🧠 React Machine Coding Problems — State (useState & Local State Basics)
These are production-level, architect-grade problems designed to test:
- State design
- UI behavior modeling
- Performance awareness
- Real-world trade-offs
1. Google Docs-like Auto-Save Editor
Requirements
- Text editor with live typing
- Auto-save after 2 seconds of inactivity
- Show status:
Saving... / Saved
UI Behavior
- Instant typing feedback
- Save indicator updates correctly
State/Data Flow
contentsaveStatusdebouncedContent
Edge Cases
- Rapid typing
- Network delay
- Save failure
Performance
- Avoid frequent API calls
- Debounce updates
Architecture
- Separate UI state vs persisted state
- Use effect cleanup for debounce
Approach
- Track
content - Debounce using
useEffect - Trigger save when stable
- Update status based on promise
2. Advanced Multi-Select Dropdown (Tagging System)
Requirements
- Select multiple options
- Search filter
- Keyboard navigation
- Create new tag
UI Behavior
- Selected tags displayed as chips
- Dropdown filters in real-time
State
selectedItemssearchQueryisOpen
Edge Cases
- Duplicate tags
- Fast typing
- Blur vs click conflicts
Performance
- Debounce search
- Avoid re-rendering all options
Architecture
- Controlled input
- Derived filtered list
Approach
- Maintain list immutably
- Toggle selection logic
3. Infinite Scroll Feed (Instagram-like)
Requirements
- Load posts as user scrolls
- Show loader at bottom
State
postspageloading
Edge Cases
- Duplicate fetch
- Fast scrolling
- API failure
Performance
- Throttle scroll events
- Avoid duplicate API calls
Architecture
- Keep fetch logic isolated
- Track request state
Approach
- Increment page on scroll
- Append data immutably
4. Undoable Form Builder
Requirements
- Add/remove fields dynamically
- Undo/redo changes
State
historycurrentIndex
Edge Cases
- Undo after new action resets redo
- Large history memory
Performance
- Avoid deep cloning unnecessarily
Architecture
- Linear history stack
Approach
- Slice history on update
- Push new state
5. Real-Time Chat Input (Optimistic UI)
Requirements
- Send message instantly
- Show pending state
- Replace with confirmed message
State
messagespendingMessages
Edge Cases
- Failed send
- Duplicate messages
Performance
- Minimal re-renders on updates
Architecture
- Temporary IDs for optimistic updates
Approach
- Add optimistic message
- Replace on success
6. Dynamic Form with Conditional Fields
Requirements
- Fields appear based on previous answers
State
formDatavisibleFields
Edge Cases
- Clearing parent removes child values
Architecture
- Derived visibility logic
Approach
- Compute visible fields from state
7. Drag & Drop List Reordering
Requirements
- Reorder items via drag
State
items
Edge Cases
- Drag cancel
- Reordering same index
Performance
- Avoid full list re-render
Approach
- Swap indices immutably
8. Search with Result Caching
Requirements
- Cache previous search results
State
querycache
Edge Cases
- Cache invalidation
Approach
9. Modal Manager (Multiple Modals)
Requirements
- Open/close multiple modals
- Only one active
State
activeModal
Edge Cases
- Rapid switching
Architecture
- Central modal state
10. Notification System (Toast Queue)
Requirements
- Queue notifications
- Auto-dismiss
State
queue
Edge Cases
- Multiple simultaneous notifications
Approach
- FIFO queue
11. File Upload with Progress
Requirements
- Show upload progress per file
State
filesprogress
Edge Cases
- Cancel upload
- Retry
12. Tab System with Persistent State
Requirements
- Each tab maintains its own state
State
activeTabtabStates
Edge Cases
- Switching tabs frequently
13. Shopping Cart with Quantity Sync
Requirements
- Update quantities
- Calculate totals
State
cartItems
Edge Cases
- Negative quantity
14. Live Filter Dashboard
Requirements
- Multiple filters
- Combined filtering logic
State
filters
Edge Cases
- Complex combinations
15. Form Draft Auto-Recovery
Requirements
- Restore form after refresh
State
formData
Approach
- Sync with localStorage
16. Pagination with Page Memory
Requirements
- Remember last page visited
State
page
17. Expandable Tree View
Requirements
- Expand/collapse nodes
State
expandedNodes
Edge Cases
- Deep nesting
18. Theme Toggle with Persistence
Requirements
- Dark/light mode
- Persist preference
State
theme
19. Real-Time Typing Indicator
Requirements
- Show “User is typing…”
State
isTyping
Edge Cases
- Debounce typing
20. Collaborative Cursor Position
Requirements
- Track cursor positions of users
State
cursors
Final Insight (Architect Level)
These problems test:- State modeling vs UI behavior
- Avoiding derived state pitfalls
- Handling async + race conditions
- Performance via minimal updates
- Scalability decisions (local vs shared state)
🧠 Senior Frontend Interview Questions — React State (useState & Local State)
Designed for FAANG-level interviews, these questions test:
- Mental models
- Trade-offs
- Debugging instincts
- Real-world decision making
1. When would you deliberately avoid using useState?
Follow-up
- What alternatives would you consider and why?
Strong Answer
AvoiduseState when:
- Value doesn’t affect UI → use
useRef - Derived data → compute instead
- Complex transitions →
useReducer - Shared state → Context/global store
useStatetriggers re-render → unnecessary cost if UI unaffected
Weak Answer
“When state is complex, use something else.”❌ Why it fails:
- Vague, lacks reasoning about render impact
2. How does React internally associate state with a component instance?
Follow-up
- What breaks this mechanism?
Strong Answer
- React uses Fiber nodes + hook call order
- Hooks stored in a linked list per component
- Order must remain consistent across renders
Weak Answer
“React tracks state by variable name.”❌ Incorrect mental model
3. Why is setState asynchronous, and what problems does that solve?
Follow-up
- What problems does it introduce?
Strong Answer
Solves:- Batching updates → performance
- Prevents intermediate inconsistent UI
- Stale state bugs
- Need for functional updates
Weak Answer
“Because React is async.”❌ No reasoning
4. Explain a real bug caused by stale closures and how you’d debug it.
Strong Answer
Example:- Uses old
count
- Log values
- Identify async boundary
Weak Answer
“Use useEffect.”❌ Doesn’t address root cause
5. How do you decide between multiple useState vs a single object state?
Follow-up
- Performance implications?
Strong Answer
Split state when:- Independent updates
- Avoid unnecessary re-renders
- Strong coupling between fields
- Granularity vs complexity
Weak Answer
“Either works.”❌ No architectural thinking
6. What are the dangers of storing derived state?
Follow-up
- When is it acceptable?
Strong Answer
Problems:- Duplication
- Sync bugs
- Expensive computations (with memoization)
Weak Answer
“It’s not recommended.”❌ Missing reasoning
7. Why does mutating state sometimes “work” but is still dangerous?
Strong Answer
- May appear to work if re-render triggered elsewhere
- Breaks React’s referential equality model
Weak Answer
“React doesn’t allow mutation.”❌ Oversimplified
8. How would you debug unnecessary re-renders caused by state?
Follow-up
- Tools and strategies?
Strong Answer
- Use React DevTools profiler
- Check state updates frequency
- Identify unnecessary updates
- Memoization
- Split components
- Avoid redundant state
Weak Answer
“Use React.memo.”❌ Tool without diagnosis
9. When should you lift state up vs keep it local?
Strong Answer
Lift when:- Shared across components
- Used in one place
- Lifting → coupling
- Local → duplication risk
Weak Answer
“Lift when needed.”❌ Too vague
10. Explain how React batches state updates in modern React.
Strong Answer
- React batches updates across events and async boundaries
- Uses scheduler to optimize rendering
Weak Answer
“React combines updates.”❌ Lacks depth
11. What is a race condition in state updates and how do you prevent it?
Strong Answer
Example:- Multiple API calls → out-of-order responses
- Track request ID / cancel previous
Weak Answer
“Use async/await.”❌ Doesn’t solve race condition
12. Why is setting state inside render dangerous?
Strong Answer
- Causes infinite loop
- Violates render purity
Weak Answer
“It’s not allowed.”❌ No explanation
13. How does useState behave differently from class setState?
Strong Answer
useStatereplaces value- Class
setStatemerges object
Weak Answer
“Hooks are better.”❌ Not technical
14. How would you design state for a complex form?
Follow-up
- Trade-offs between flat vs nested state?
Strong Answer
- Flat structure preferred
- Easier updates and validation
Weak Answer
“Use one object.”❌ No nuance
15. Explain a scenario where state causes performance bottleneck.
Strong Answer
- Frequent updates (e.g., typing)
- Large component tree re-render
- Debounce
- Split components
Weak Answer
“Too many states.”❌ Not specific
16. Why does React recommend immutability?
Strong Answer
- Enables efficient diffing
- Predictable updates
Weak Answer
“Because React says so.”❌ No reasoning
17. How would you handle state that depends on previous state in async flows?
Strong Answer
- Always use functional updates
Weak Answer
“Use useEffect.”❌ Not addressing dependency issue
18. What are common mistakes when syncing props to state?
Strong Answer
- State doesn’t update automatically
- Causes stale UI
- Avoid duplication or use effect
Weak Answer
“Use state for props.”❌ Misunderstanding
19. How would you model state for a real-time UI (chat, typing indicator)?
Strong Answer
- Separate transient vs persistent state
- Handle race conditions
- Minimize re-renders
Weak Answer
“Use WebSocket.”❌ Not about state design
20. What signals indicate your state design is flawed?
Strong Answer
- Frequent bugs syncing values
- Too many dependencies in effects
- Re-renders hard to control
- Duplicate data
Weak Answer
“Code is messy.”❌ Not actionable
Final Interview Insight
A senior engineer should demonstrate:- Mental model of React internals
- Clear reasoning behind decisions
- Ability to debug real-world issues
- Trade-off awareness (performance vs simplicity)