📖 Scenario: You are building a simple React app that shows a list of books with their authors. To keep your code clean and easy to maintain, you want to separate the data, configuration, and display logic into different parts.
🎯 Goal: Build a React app that uses separate components and variables for the book data, a configuration for filtering, and a component that shows only books matching the filter.
📋 What You'll Learn
Create a list of books as an array of objects with exact titles and authors
Add a configuration variable to filter books by author name
Use a React functional component to filter and display the books based on the configuration
Use a separate child component to render each book's title and author
💡 Why This Matters
🌍 Real World
Separating concerns helps developers build React apps that are easier to read, test, and update. It is common to keep data, configuration, and UI components separate.
💼 Career
Understanding separation of concerns is essential for React developers to write maintainable code and collaborate effectively in teams.
Progress0 / 4 steps
1
Create the book data array
Create a constant array called books with these exact objects: { title: 'The Hobbit', author: 'J.R.R. Tolkien' }, { title: '1984', author: 'George Orwell' }, and { title: 'To Kill a Mockingbird', author: 'Harper Lee' }.
React
Hint
Use const books = [ ... ] with objects inside the array.
2
Add a filter configuration variable
Create a constant string called filterAuthor and set it to 'George Orwell'.
React
Hint
Use const filterAuthor = 'George Orwell'; to set the filter.
3
Filter and display books in a React component
Create a React functional component called BookList that filters the books array using filterAuthor and returns a <div> containing a list of <BookItem> components for each filtered book. Use book.title and book.author as props for <BookItem>. Use book.title as the key.
React
Hint
Use books.filter inside BookList and map to <BookItem>.
4
Create the BookItem component
Create a React functional component called BookItem that accepts title and author as props and returns a <div> containing the book title in a <strong> and the author in a <em>.
React
Hint
Use props { title, author } and return a <div> with <strong> and <em>.
Practice
(1/5)
1. What does separation of concerns mean in React development?
easy
A. Dividing the app so each part handles one specific task
B. Writing all code in a single component for simplicity
C. Mixing UI and data logic in the same function
D. Using only class components instead of functions
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 A
Quick Check:
Separation of concerns = dividing tasks clearly [OK]
Hint: Think: one part, one job in your app [OK]
Common Mistakes:
Thinking all code should be in one component
Mixing UI and logic in the same place
Confusing separation with using classes
2. Which of the following is the correct way to separate UI and logic in React?
easy
A. Put all UI and logic inside one component's render method
B. Create a UI component and a separate hook for logic
C. Write logic inside HTML tags directly
D. Use inline styles to handle logic
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 B
Quick Check:
UI + logic separated = Create a UI component and a separate hook for logic [OK]
Hint: Use hooks for logic, components for UI [OK]
Common Mistakes:
Putting logic inside render method
Trying to write logic inside HTML
Using styles to control logic
3. Given this React code, what will be the output?
function UserName({ name }) {
return <h1>User: {name}</h1>;
}
function UserLogic() {
const user = { name: 'Anna' };
return <UserName name={user.name} />;
}
medium
A. <h1>User: </h1>
B. Error: user is undefined
C. <h1>User: Anna</h1>
D. UserName component not found
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 C
Quick Check:
Prop passed correctly = <h1>User: Anna</h1> [OK]
Hint: Follow props from parent to child component [OK]
Common Mistakes:
Assuming user is undefined outside UserLogic
Expecting empty output if prop missing
Confusing component names
4. What is wrong with this React code regarding separation of concerns?