In a React Single Page Application (SPA), clicking a navigation link usually does not reload the whole page. What is the main behavior you observe?
Think about how SPAs avoid full page reloads to make navigation faster.
In React SPAs, navigation changes the URL and React updates the displayed components without reloading the entire page. This makes the app feel faster and smoother.
Consider this React component snippet inside a SPA:
const [count, setCount] = useState(0);
function increment() {
setCount(count + 1);
}
increment();
increment();
console.log(count);What will console.log(count) output immediately after calling increment() twice?
const [count, setCount] = useState(0); function increment() { setCount(count + 1); } increment(); increment(); console.log(count);
Remember that state updates in React are asynchronous and do not immediately change the variable.
React state updates are asynchronous. Calling setCount twice quickly does not immediately update the count variable. The logged value remains the initial 0.
In React Router v6, which code snippet correctly defines a route that renders HomePage component at path /home?
React Router v6 uses the element prop to specify the component to render.
In React Router v6, routes use the element prop with JSX to render components. The other props are from older versions or invalid.
Look at this React component code:
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
setCount(count + 1);
});
return <div>Count: {count}</div>;
}Why does this cause an infinite render loop?
function Counter() { const [count, setCount] = useState(0); useEffect(() => { setCount(count + 1); }); return <div>Count: {count}</div>; }
Think about how useEffect dependencies control when the effect runs.
Without a dependency array, useEffect runs after every render. Calling setCount inside it triggers another render, causing an infinite loop.
Choose the best explanation for why SPAs are often preferred over traditional multi-page applications.
Think about how SPAs handle page changes compared to full reloads.
SPAs load the app once and update content dynamically without full page reloads, making navigation faster and smoother. Other options are incorrect or misleading.