React State Management 1 — Questions and Answers
Question 1: What is lifting state up in React?
- Moving state to a CDN
- Moving shared state to the closest common ancestor component so multiple children can access it (Correct answer)
- Increasing state values
- A performance optimization
Correct answer: Moving shared state to the closest common ancestor component so multiple children can access it
When multiple components need the same data, the state is moved to their nearest common parent, which passes it down via props.
Question 2: What is the Context API?
- A REST API
- A React feature for sharing data globally across components without passing props through every level (Correct answer)
- A third-party library
- A server-side feature
Correct answer: A React feature for sharing data globally across components without passing props through every level
Context provides a way to share values (like themes, user data, preferences) across the component tree without explicit prop passing at every level.
Question 3: What is useReducer?
- A size reducer
- A hook for managing complex state logic with a reducer function, similar to Redux patterns (Correct answer)
- A file compressor
- A performance tool
Correct answer: A hook for managing complex state logic with a reducer function, similar to Redux patterns
useReducer is an alternative to useState for complex state logic. It uses a reducer function that takes current state and an action, returning new state.
Question 4: What triggers a re-render in React?
- Every second automatically
- State changes via setState/useState, parent re-renders passing new props, or context value changes (Correct answer)
- Only manual refresh
- DOM changes
Correct answer: State changes via setState/useState, parent re-renders passing new props, or context value changes
React re-renders when state changes, when a parent passes different props, or when consumed context values change. This triggers reconciliation with the virtual DOM.
Question 5: What is immutability in React state?
- State that cannot exist
- The practice of creating new objects/arrays instead of modifying existing ones when updating state (Correct answer)
- Read-only components
- A security feature
Correct answer: The practice of creating new objects/arrays instead of modifying existing ones when updating state
React relies on reference comparison to detect changes. Mutating state directly does not trigger re-renders. Creating new objects/arrays ensures React detects the change.
Question 6: What is Redux?
- A React component
- A predictable state management library using a single store, actions, and reducers for managing application state (Correct answer)
- A CSS framework
- A database
Correct answer: A predictable state management library using a single store, actions, and reducers for managing application state
Redux centralizes application state in a single store. Components dispatch actions that are processed by reducers to produce new state, making state changes predictable.
What is lifting state up in React?