0
0
Reactframework~20 mins

Separation of concerns in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Separation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
How does separation of concerns improve React component design?

Consider a React app where UI, data fetching, and state management are all mixed in one component. What is the main benefit of separating these concerns into different components or hooks?

AIt reduces the number of components, making the app faster to load.
BIt allows mixing UI and data logic freely without any structure.
CIt makes the code easier to read, test, and maintain by isolating responsibilities.
DIt forces all logic to be written inside the main App component for simplicity.
Attempts:
2 left
💡 Hint

Think about how breaking tasks into smaller parts helps in real life, like organizing a kitchen by utensils, ingredients, and appliances.

state_output
intermediate
1:30remaining
What is the output of this React component with separated concerns?

Given the following React components, what will be rendered on the screen?

React
function useCounter() {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    const timer = setInterval(() => setCount(c => c + 1), 1000);
    return () => clearInterval(timer);
  }, []);
  return count;
}

function CounterDisplay() {
  const count = useCounter();
  return <div>Count: {count}</div>;
}
AA div showing 'Count: 0' initially, then increasing by 1 every second.
BA div showing 'Count: 0' that never changes.
CA div showing 'Count: NaN' due to incorrect state initialization.
DNothing renders because useCounter is not a valid hook.
Attempts:
2 left
💡 Hint

Look at how useCounter updates state every second and returns the count.

📝 Syntax
advanced
2:00remaining
Which option correctly separates data fetching from UI in React?

Choose the code snippet that properly separates data fetching logic from UI rendering using hooks.

A
function DataComponent() {
  const [data, setData] = React.useState(null);
  fetch('/api/data').then(res =&gt; res.json()).then(setData);
  if (!data) return &lt;div&gt;Loading...&lt;/div&gt;;
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
B
function useData() {
  const [data, setData] = React.useState(null);
  React.useEffect(() =&gt; {
    fetch('/api/data').then(res =&gt; res.json()).then(setData);
  }, []);
  return data;
}

function DataComponent() {
  const data = useData();
  if (!data) return &lt;div&gt;Loading...&lt;/div&gt;;
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
C
function DataComponent() {
  const data = fetch('/api/data').then(res =&gt; res.json());
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
D
function useData() {
  const data = fetch('/api/data').then(res =&gt; res.json());
  return data;
}

function DataComponent() {
  const data = useData();
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
Attempts:
2 left
💡 Hint

Remember that data fetching should happen inside useEffect to avoid infinite loops and to handle async properly.

🔧 Debug
advanced
2:00remaining
Why does this React component violate separation of concerns?

Examine the code below. What is the main problem related to separation of concerns?

React
function UserProfile() {
  const [user, setUser] = React.useState(null);
  React.useEffect(() => {
    fetch('/api/user').then(res => res.json()).then(setUser);
  }, []);

  function handleClick() {
    alert(`User: ${user.name}`);
  }

  return (
    <div>
      <h1>{user ? user.name : 'Loading...'}</h1>
      <button onClick={handleClick}>Show Name</button>
    </div>
  );
}
AThe component mixes data fetching, UI rendering, and event handling all in one place, making it harder to maintain.
BThe alert inside handleClick causes a runtime error if user is null.
CThe component does not fetch data correctly because useEffect is missing dependencies.
DThe component should not use useState for user data.
Attempts:
2 left
💡 Hint

Think about how separating data fetching, UI, and event logic into different parts helps clarity.

🧠 Conceptual
expert
2:30remaining
What is the best practice for separation of concerns in a large React app?

In a large React application, which approach best follows separation of concerns principles?

AUse only class components to keep state and UI tightly coupled.
BPut all logic and UI in one component to reduce the number of files and simplify imports.
CWrite all data fetching in the root App component and pass data down as props everywhere.
DUse custom hooks for data and state logic, presentational components for UI, and container components to connect them.
Attempts:
2 left
💡 Hint

Think about dividing tasks like a team where each member has a clear role.