The primary goal of this exercise is not just to fix bugs, but to demonstrate your process for identifying faults, analyzing code quality, and implementing modern, maintainable JavaScript/React practices.
This scenario simulates a real-world task where you inherit a piece of poorly-written and broken legacy code that needs to be brought up to production standards.
You have been provided with two files:
-
apiService.js: This simulates a simple Node.js/Express backend service by returning a Promise. It contains intentional bugs, specifically related to error handling and data consistency. -
BuggyProjectList.jsx: This is a React functional component that attempts to consume thefetchProjectsfunction from the service file. It contains several major anti-patterns and bugs, including issues related touseEffect, state management, and data access.
Please approach this exercise in three phases. Focus on communicating your thoughts and decisions out loud throughout the process.
-
Identify all bugs in both
apiService.jsandBuggyProjectList.jsx. -
Fix the bugs to ensure the component runs correctly, fetches data reliably, and properly handles both successful responses and error states.
-
Ensure the component correctly applies the
filterTermprop.
Once the component is functional, refactor the code to improve quality and adherence to modern best practices:
-
Extract Logic into a Custom Hook: The data fetching, loading state, and error handling logic within
BuggyProjectListis too complex for a presentational component. Extract this logic into a clean, reusable custom hook (e.g.,useProjectsData(filterTerm)). -
Robust State Management: Ensure the component clearly handles and displays the four possible states: Loading, Success (with data), Error, and Empty (no results after filtering).
Be prepared to discuss your refactored code and architectural choices, including:
-
The reasoning behind your custom hook implementation.
-
How you would approach unit testing both the custom hook and the component logic.
-
Trade-offs you considered (e.g., synchronous vs. asynchronous logic, choosing state management patterns).
This is a flexible guideline for our 60-minute session:
| Duration | Activity |
|---|---|
| 5 min | Review the files and ask clarifying questions. |
| 20 min | Phase 1: Debugging and Initial Fixes |
| 30 min | Phase 2: Refactoring into a Custom Hook |
| 5 min | Phase 3: Review and Discussion |