useState in Functional Components
Which hook would you use to add state to a functional component in React?
- useState
- useStates
- userState
- useEffect
- useStat
Lifting State Up
When two sibling components both need to access or update the same state, what is the recommended approach in React?
- Lift the state up to the nearest common ancestor
- Duplicate the state in each sibling
- Use local state in each sibling only
- UseState directly in both siblings
- Pass functions from root to each child independently
Sharing State Across Deeply Nested Components
Which React feature allows you to avoid 'prop drilling' when many nested components need access to the same state?
- Context API
- Lifecycle Methods
- Hooks API
- usePropType
- Updater Function
Updating State Based on Previous Value
Given the code `setCount(count + 1);`, what problem might you encounter when updating state multiple times in a row?
- Stale state issues can cause unexpected values
- It triggers an infinite render loop
- It mutates the state directly
- It re-initializes the entire component
- State is updated asynchronously and skipped
Correct State Update Pattern
What is the recommended way to update state based on its previous value in React?
- setState(prev =u003E prev + 1)
- setState(state + 1)
- setState = prev =u003E prev + 1
- setStat(prev =u003E prev + 1)
- updateState(prev + 1)
PropTypes Purpose
Why might you use PropTypes in your React components?
- To validate the types of props and catch errors early
- To manage component local state
- To fetch data asynchronously
- To enforce strict mode globally
- To avoid rendering child components
useEffect and State
Which of the following is a primary use case for the useEffect hook in relation to state?
- Running side effects when state changes
- Initializing prop types
- Rendering components conditionally
- Directly mutating state values
- Avoiding the use of props
Props vs State
How do props and state differ in React components?
- State is mutable and managed internally, props are immutable and passed from parents
- Both are read-only inside components
- Props are used for local data, state for external data only
- State can only be updated by parent components
- Props cannot contain numbers or strings
Avoiding Unnecessary Rerenders
Which hook can help memoize a function to prevent unnecessary rerenders when passing it as a prop?
- useCallback
- useEffact
- useMemorize
- useMomo
- useCallbak
Best Practice for Derived State
If a component's data can be calculated from props, what is the recommended method for handling this in React?
- Calculate the value directly in the render or return method
- Store the derived value in state and update it manually
- Pass it through context
- Use localStorage for calculation
- Avoid using calculated values