Given a repository class that fetches user data, what will the component display?
class UserRepository { async getUser() { return { id: 1, name: 'Alice' }; } } import React, { useEffect, useState } from 'react'; export default function UserProfile() { const [user, setUser] = useState(null); useEffect(() => { const repo = new UserRepository(); repo.getUser().then(setUser); }, []); if (!user) return <p>Loading...</p>; return <p>{`User: ${user.name}`}</p>; }
Think about when the data is fetched and how state updates.
The component starts with user as null, so it shows 'Loading...'. After getUser() resolves, user updates and the component shows 'User: Alice'.
Choose the correct syntax for an async method in a repository class that fetches posts from an API.
class PostRepository { async fetchPosts() { // implementation } }
Remember to use await with async calls and return the JSON data.
Option B correctly marks the method as async, awaits the fetch call, and returns the parsed JSON. Option B misses async keyword, so await causes syntax error. Option B misses await on fetch, so res.json() is called on a Promise, causing error. Option B returns text instead of JSON.
Identify the cause of the error in this repository method.
class ProductRepository { async getProducts() { const response = await fetch('/api/products'); if (!response.ok) throw new Error('Failed to fetch'); return response.json; } }
Look carefully at how the JSON data is returned.
The method returns response.json which is a function reference, not the parsed JSON data. It should call response.json() to get the data.
Given this component using a repository, what is the value of data after the effect runs?
class CommentRepository { async fetchComments() { return [{ id: 1, text: 'Hi' }, { id: 2, text: 'Hello' }]; } } import React, { useEffect, useState } from 'react'; export default function Comments() { const [data, setData] = useState([]); useEffect(() => { const repo = new CommentRepository(); repo.fetchComments().then(setData); }, []); return null; }
Consider what the repository method returns and how state is set.
The repository method returns an array of two comment objects. The component sets data to this array after the promise resolves.
Choose the most accurate explanation of why the repository pattern is useful in Next.js applications.
Think about separation of concerns and code organization.
The repository pattern groups data access code in one place. This separation helps maintainability and testing. It does not automatically cache data, replace hooks, or restrict fetching to server side only.