Restaurant list screen
You build the restaurant list screen with cached, paged restaurant data, pull-to-refresh and automatic refetch when the app returns online or to the foreground.
Ticket
Build the restaurant list screen in app/. It loads restaurants near the fixed Week 2 seed point from GET /v1/restaurants, one page at a time. Customers can scroll through the results, pull to refresh, and open a restaurant's menu.
The screen gives customers the starting point for choosing what to order. The next exercise, W4B4, builds the menu screen that opens when a customer selects a restaurant. The backend does not change.
Acceptance criteria
Page<T>represents a page withitemsandnext_cursor.nextCursorreturnsundefinedwhennext_cursoris empty, and returns the cursor otherwise.flattencombines theitemsfrom all pages in their original order. It returns an empty array when there are no pages.restaurantsPathalways includeslat=28.46,lng=77.03,radius_km=5andlimit=20for the fixed seed point. It addscursoronly when the cursor is not empty, usingURLSearchParamsto encode it.fetchRestaurantssends the path through the API client. It does not callfetchdirectly.openLabelreturns"Open"whenis_openistrue,"Closed"when it isfalse, andnullwhen the API does not provideis_open.- The infinite query uses the query key
["restaurants", lat, lng, radiusKm],initialPageParam: "",getNextPageParam: nextCursorandstaleTime: 60_000. - The shared TanStack Query client connects
focusManagerto React NativeAppStateandonlineManagertoexpo-network. Queries retry network failures and5xxresponses at most twice, and never retry4xxresponses. - The query client is created once at module level in
app/lib/queryClient.ts. The root layout uses that instance. - The screen uses FlatList for virtualization, not a
ScrollViewwithmap. Rows use the restaurant id as their key.renderItemand the row-opening callback use stableuseCallbackreferences, andRestaurantRowusesReact.memo. - Every restaurant row is exactly 76 dp high, including its bottom border.
getItemLayoutuses that height for its item length and offset calculations. onEndReacheduses a threshold of0.5. It callsfetchNextPage()only whenhasNextPageis true andisFetchingNextPageis false.- The pull-to-refresh control stays active during a refetch, but not during a next-page fetch. A footer spinner appears while the next page loads.
- The first pending load displays eight skeleton rows. An error with no data displays the full-screen error and a Try again button. An empty result displays the centred empty state.
- A failed background refetch or pull-to-refresh keeps the existing restaurants visible. The full-screen error is reserved for an error with no data.
- Tapping a restaurant pushes
/restaurant/[id]. - The root layout imports the shared query client. The tab layout places Sign out in the tab header's
headerRight.
Constraints
- Use latitude
28.46, longitude77.03and radius5km for the fixed seed point. - Set
PAGE_SIZEto20. - Set
staleTimeto60_000milliseconds. - Limit query retries to two for network failures and
5xxresponses. Never retry4xxresponses. - Set the row height to
76dp andonEndReachedThresholdto0.5. - Show eight skeleton rows during the first pending load.
- The Week 2 seed contains 50 restaurants, which produces three pages at a page size of 20.
Design direction
Keep the data transformations separate from React components. Put Page<T>, nextCursor and flatten in app/lib/pages.ts. Put the restaurant types, path construction, API request and open label in app/lib/restaurants.ts. These functions are easier to test without rendering a screen.
Put the paging configuration in app/lib/useRestaurants.ts. Keep the shared query client and the app's focus and online event wiring in app/lib/queryClient.ts. Let the root layout provide that client to the app.
Keep the screen focused on presentation. Put the row and its skeleton in app/components/RestaurantRow.tsx, and the loading, error and empty states in app/components/StateViews.tsx. The screen should flatten the query's pages and pass the resulting restaurants to FlatList.
Consider these trade-offs:
- Keep the query's pages intact for pagination, then flatten them for rendering. This preserves the server's page boundaries without making the list manage cursors.
- Include every location input in the query key. A different radius or coordinate must not reuse another area's cached result.
- A fixed-height row makes
getItemLayoutpredictable. Keep the bottom border inside the row so it does not add to the measured height. - Keep background errors separate from initial-load errors. A failed refresh should not discard restaurants the customer can already see.
- Connect focus and network events once through the shared query client. Later screens can then use the same behaviour without installing separate listeners.
Any layout that meets the contract passes.
How to check your work
Run the library tests first, then check TypeScript and launch the app:
cd backend && make test W=4B3cd app && npx tsc --noEmitcd app && npx expo startOn your phone, check the initial skeletons, restaurant rows, next-page spinner, pull-to-refresh, offline recovery and menu navigation. To see pagination more clearly, temporarily set the page size to 5, then restore it to 20. When a check fails, read the first failing assertion to identify the specific contract that your implementation violates before changing anything else.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Add page helpers and restaurant request functions.
- Configure the shared query client and infinite query.
- Build the memoised 76 dp restaurant row and state views.
- Implement the paginated FlatList and pull-to-refresh behaviour.
- Verify focus, reconnect and background error handling.
- Run the W4B3 tests and check the app on a device.
Commands
cd backend && make test W=4B3cd app && npx tsc --noEmitcd app && npx expo startTests
W4 B3 restaurant list (6 tests)inapp-tests/lib/restaurants.test.ts