Separation of concerns means keeping different parts of your code focused on one job. This makes your app easier to understand and fix.
Separation of concerns in React
function ComponentA() { // Handles UI only return <div>UI here</div>; } function ComponentB() { // Handles data or logic const data = fetchData(); return <ComponentA data={data} />; }
function Button({ onClick, label }) { return <button onClick={onClick}>{label}</button>; }
function App() { const handleClick = () => alert('Clicked!'); return <Button onClick={handleClick} label="Click me" />; }
This Counter app splits the UI and logic into separate parts. CounterDisplay only shows the count. CounterButtons only shows buttons and calls functions when clicked. The main Counter component holds the count state and logic to change it.
This makes the code clear and easy to change or reuse parts.
import React, { useState } from 'react'; function CounterDisplay({ count }) { return <h1>Count: {count}</h1>; } function CounterButtons({ onIncrement, onDecrement }) { return ( <div> <button onClick={onIncrement} aria-label="Increase count">+</button> <button onClick={onDecrement} aria-label="Decrease count">-</button> </div> ); } export default function Counter() { const [count, setCount] = useState(0); const increment = () => setCount(c => c + 1); const decrement = () => setCount(c => c - 1); return ( <main> <CounterDisplay count={count} /> <CounterButtons onIncrement={increment} onDecrement={decrement} /> </main> ); }
Keep components small and focused on one job.
Use props to share data and actions between components.
Separation helps with testing, reusing, and fixing code.
Separation of concerns means each part of your app does one clear thing.
Use small components for UI and separate logic or data handling.
This makes your React app easier to read, test, and maintain.
Practice
separation of concerns mean in React development?Solution
Step 1: Understand the concept of separation of concerns
It means each part of the app should focus on one clear responsibility, like UI or data handling.Step 2: Identify the correct description
Dividing the app so each part handles one specific task correctly describes dividing the app so each part handles one specific task, which matches the concept.Final Answer:
Dividing the app so each part handles one specific task -> Option AQuick Check:
Separation of concerns = dividing tasks clearly [OK]
- Thinking all code should be in one component
- Mixing UI and logic in the same place
- Confusing separation with using classes
Solution
Step 1: Identify proper separation of UI and logic
React encourages using components for UI and hooks or functions for logic.Step 2: Match options to this pattern
Create a UI component and a separate hook for logic correctly separates UI in a component and logic in a hook, following best practices.Final Answer:
Create a UI component and a separate hook for logic -> Option BQuick Check:
UI + logic separated = Create a UI component and a separate hook for logic [OK]
- Putting logic inside render method
- Trying to write logic inside HTML
- Using styles to control logic
function UserName({ name }) {
return <h1>User: {name}</h1>;
}
function UserLogic() {
const user = { name: 'Anna' };
return <UserName name={user.name} />;
}Solution
Step 1: Trace the data flow from UserLogic to UserName
UserLogic creates a user object with name 'Anna' and passes user.name as prop to UserName.Step 2: Check UserName output
UserName receives name='Anna' and renders <h1>User: Anna</h1> correctly.Final Answer:
<h1>User: Anna</h1> -> Option CQuick Check:
Prop passed correctly = <h1>User: Anna</h1> [OK]
- Assuming user is undefined outside UserLogic
- Expecting empty output if prop missing
- Confusing component names
function Profile() {
const [user, setUser] = React.useState(null);
React.useEffect(() => {
fetch('/api/user')
.then(res => res.json())
.then(data => setUser(data));
}, []);
return <div>{user.name}</div>;
}Solution
Step 1: Identify concerns in the Profile component
Profile handles both fetching user data and rendering UI, mixing logic and UI.Step 2: Understand separation of concerns principle
Best practice is to separate data fetching logic from UI components, e.g., using custom hooks.Final Answer:
Fetching data and UI rendering are mixed in one component -> Option AQuick Check:
Mixed logic and UI = Fetching data and UI rendering are mixed in one component [OK]
- Thinking useEffect is wrong here
- Believing state can't be used in functions
- Assuming fetch URL is the problem
function Dashboard() {
const [data, setData] = React.useState(null);
React.useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(json => setData(json));
}, []);
return <div>{data ? <Display data={data} /> : 'Loading...'}</div>;
}Which change best improves separation of concerns?
Solution
Step 1: Analyze current code responsibilities
Dashboard handles both fetching data and rendering UI, mixing concerns.Step 2: Choose best separation approach
Moving data fetching to a custom hook isolates logic, leaving Dashboard focused on UI.Final Answer:
Move data fetching into a custom hook and keep Dashboard for UI only -> Option DQuick Check:
Separate logic in hooks, UI in components = Move data fetching into a custom hook and keep Dashboard for UI only [OK]
- Merging components reduces separation
- Fetching data inside UI components mixes concerns
- Removing useEffect breaks async data loading
Ready to prove it?
Pass the test and this pattern moves unknown
