0
0
NextJSframework~20 mins

Repository pattern for data access in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Repository Mastery in Next.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Next.js repository component render?

Given a repository class that fetches user data, what will the component display?

NextJS
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>;
}
AComponent throws an error
B<p>User: undefined</p>
C<p>Loading...</p> forever
D<p>Loading...</p> initially, then <p>User: Alice</p>
Attempts:
2 left
💡 Hint

Think about when the data is fetched and how state updates.

📝 Syntax
intermediate
2:00remaining
Which option correctly implements a Next.js repository method to fetch posts?

Choose the correct syntax for an async method in a repository class that fetches posts from an API.

NextJS
class PostRepository {
  async fetchPosts() {
    // implementation
  }
}
AfetchPosts() { const res = await fetch('/api/posts'); return res.json(); }
Basync fetchPosts() { const res = await fetch('/api/posts'); return res.json(); }
Casync fetchPosts() { const res = fetch('/api/posts'); return res.json(); }
Dasync fetchPosts() { const res = await fetch('/api/posts'); return res.text(); }
Attempts:
2 left
💡 Hint

Remember to use await with async calls and return the JSON data.

🔧 Debug
advanced
2:00remaining
Why does this Next.js repository method cause a runtime error?

Identify the cause of the error in this repository method.

NextJS
class ProductRepository {
  async getProducts() {
    const response = await fetch('/api/products');
    if (!response.ok) throw new Error('Failed to fetch');
    return response.json;
  }
}
AIt returns response.json instead of calling response.json()
BIt does not check if response.ok is true
Cfetch is missing await keyword
DThe method is not marked async
Attempts:
2 left
💡 Hint

Look carefully at how the JSON data is returned.

state_output
advanced
2:00remaining
What is the state value after this Next.js repository fetch in a React component?

Given this component using a repository, what is the value of data after the effect runs?

NextJS
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;
}
A[{ id: 1, text: 'Hi' }, { id: 2, text: 'Hello' }]
B[]
Cnull
D[{ id: 1, text: 'Hi' }]
Attempts:
2 left
💡 Hint

Consider what the repository method returns and how state is set.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the benefit of using the repository pattern in Next.js data access?

Choose the most accurate explanation of why the repository pattern is useful in Next.js applications.

AIt replaces React hooks for managing component state with a global store.
BIt automatically caches all API responses to improve performance without extra code.
CIt centralizes data fetching logic, making it easier to maintain and test data access separately from UI components.
DIt forces all data fetching to happen on the server side only, preventing client-side requests.
Attempts:
2 left
💡 Hint

Think about separation of concerns and code organization.