Identifying State in React Components
Which of the following is the best indicator that a value should be stored in React state?
- The value changes over time and affects the UI
- The value is constant throughout the app's lifetime
- The value comes from props and is never modified
- The value can be computed from other variables at any time
- The value doesn't affect rendering
Lifting State Up Purpose
Why would you 'lift state up' in a React application?
- To share the same data between multiple child components
- To allow each child component to store its own private state
- To simplify code by placing all logic in one component
- To prevent prop drilling by using context instead
- To reduce re-renders in child components
State Update Syntax
Which is the correct way to update 'count' in React state using the useState hook?
- setCount(count + 1);
- count = count + 1;
- this.state.count += 1;
- setState({count: count + 1});
- updateCount(addOne(count));
Identifying When to Lift State Up
Given two sibling components needing to access and update the same data, what should you do?
- Move the state to their common parent component
- Copy the state to both siblings
- Store the shared state in each component's props
- Use local variables instead of state
- Place the state inside one sibling and pass it as props to the other
State vs. Props
Which statement below is TRUE about props and state in React?
- Props are passed into a component, while state is managed within the component
- State can be passed from parent to child as is
- Props can only hold numbers and strings
- State values can only be set during component initialization
- Props and state always change in sync
Side Effects in State Management
Which React hook should you use to handle side effects when a state value changes?
- useEffect
- useSharedState
- useCallback
- useReducer
- useStore
Two-way Binding and Lifting State Up
A parent component manages 'inputValue' state and passes it to a child input. How should the child update this value?
- Call a function from props that updates the parent state
- Directly set the parent's state from the child
- Use useContext to access the value
- Change its own local state only
- Set props.inputValue directly in the child
Typo in 'setSate'
What happens if you accidentally call 'setSate' instead of 'setState' in a class component?
- You get an error because 'setSate' is not defined
- The state updates normally
- React will warn but continue
- A runtime warning is shown, but nothing breaks
- Nothing happens and the code works as expected
State Lifting and Performance
Which can be a possible downside of lifting state up unnecessarily?
- Causes unnecessary re-renders of child components
- Helps components have perfectly isolated states
- Reduces the amount of code in parent components
- Prevents children from communicating
- Always improves performance
Best Practice after Lifting State Up
After lifting state up to a common parent, how should you organize callback functions for updating the state?
- Define the state-updating functions in the parent and pass them down as props
- Define update functions in each child separately
- Use global variables for updates
- Attach event listeners directly to DOM elements
- Let children update parent state by accessing it directly