Consider a large React app using code splitting with React.lazy and Suspense. What is the main effect on the app's initial load?
const LazyComponent = React.lazy(() => import('./HeavyComponent')); function App() { return ( <React.Suspense fallback={<div>Loading...</div>}> <LazyComponent /> </React.Suspense> ); }
Think about how loading parts of the app only when needed can help speed up the start.
Code splitting with React.lazy loads only the code needed initially, deferring heavy components until they are needed. This reduces the initial bundle size and speeds up loading.
Given a React context managing a counter, what is the final value of count after these updates?
const CountContext = React.createContext(); function CounterProvider({ children }) { const [count, setCount] = React.useState(0); React.useEffect(() => { setCount(c => c + 1); setCount(c => c + 1); setCount(c => c + 1); }, []); return <CountContext.Provider value={count}>{children}</CountContext.Provider>; } function Display() { const count = React.useContext(CountContext); return <div>{count}</div>; }
Remember that using the updater function form of setState batches updates correctly.
Using the updater function c => c + 1 three times in one effect results in the count increasing by 3 because React batches these updates and applies them sequentially.
This React app freezes when updating a large list in state. What is the main cause?
function LargeList() { const [items, setItems] = React.useState(Array(100000).fill(0)); function updateItems() { const newItems = items.map(x => x + 1); setItems(newItems); } return <button onClick={updateItems}>Update</button>; }
Think about what happens when you create a new large array and React re-renders.
Updating a very large array by creating a new one and setting it in state causes React to re-render the whole list, which can freeze the app due to heavy computation and rendering.
Given a React context with a default value, which code correctly accesses the context value in a functional component using TypeScript?
const UserContext = React.createContext<{name: string} | null>(null); function UserName() { // Access context here }
Consider how to safely assert the context is not null when you know it will be provided.
Using the non-null assertion operator ! tells TypeScript the context value is not null, allowing safe access to user.name. Option B causes a TypeScript error because user might be null.
In very large React applications, why do developers often choose to use Redux or similar libraries?
Think about how managing state across many components can get complicated.
Redux centralizes state in one place, making it easier to track, update, and debug state changes in large apps. It does not automatically optimize rendering or replace React state entirely.