DrillLab
前端 · 考试Course

React Cab Booking(Context 版)React Cab Booking (Context version)

一个用 Context 管全局状态的打车小应用。四个页面、一个 Context、四个测试。练的是「Context 在一个真实多页应用里怎么用」—— Provider 放在哪一层、一个 action 同时改两个 state、消费者散在三个组件里。A small cab booking app that keeps its global state in a Context. Four pages, one Context, four tests. The practice here is how to use Context in a real app with several pages: which level the Provider goes on, one action updating two pieces of state at once, and readers of the Context spread across three components.

React 18ContextViteVitestReact Testing Library
这门考试到底考什么What this exam actually tests

Context 三件套与 Provider 的层级、一次更新两个 state、页面状态机、数组取尾部三条并反转、useEffect 的 setTimeout 清理、data-testid 契约。附带一个真实的脚手架缺陷:完整答案原样跑不起来。The three parts of Context and the level the Provider sits at, updating two pieces of state in one action, the page state machine, taking the last three items of an array and reversing them, clearing a setTimeout inside useEffect, and the data-testid contract. Plus one real defect in the provided project: the complete answer does not run as given.

内容来源 · 你本机上的真实项目Where the content comes from · the real project on your machine
源项目Source project1 项 · 1 个可以展开看原文1 items · 1 can be opened
cab-booking-context参考项目。6 个组件 + 1 个 Context + 4 个测试Reference project. Six components, one Context, four tests
Textcab-booking-context/ · tree源项目From source
1cab-booking-context/
2├── public/
3│ └── cabs/
4│ ├── bmw-5-series.svg
5│ ├── ford-explorer.svg
6│ ├── ford-fusion.svg
7│ ├── honda-accord.svg
8│ ├── mercedes-e-class.svg
9│ └── toyota-highlander.svg
10├── src/
11│ ├── components/
12│ │ ├── CabConfirmation/
13│ │ │ └── CabConfirmation.jsx
14│ │ ├── CabOptions/
15│ │ │ ├── CabCard.jsx
16│ │ │ └── CabOptions.jsx
17│ │ ├── Home/
18│ │ │ ├── Home.jsx
19│ │ │ └── RideHistory.jsx
20│ │ ├── Loading/
21│ │ │ └── Loading.jsx
22│ │ └── AppHeader.jsx
23│ ├── context/
24│ │ └── CabContext.js
25│ ├── data/
26│ │ └── data.json
27│ ├── styles/
28│ │ └── app-ui.css
29│ ├── test/
30│ │ ├── App.test.jsx
31│ │ └── setup.js
32│ ├── App.css
33│ ├── App.jsx
34│ ├── index.css
35│ └── index.jsx
36├── .gitignore
37├── index.html
38├── package.json
39├── README.md
40└── vite.config.mjs
Source: cab-booking-context
先修Prerequisite

建议先过一遍Work through 地基 · 项目与语言。如果你已经会 npm、会读 package.json,可以直接开始这一门。 first. If you already know npm and can read a package.json, start here directly.

真实任务覆盖清单Coverage of the real tasks
真实项目里的任务Task in the real projectDrillLab 讲到哪Where DrillLab covers it有测试Tested
首页 + 空历史提示第 1 部分 · 先读四个测试Yes
Context 三件套 + Provider 层级第 1 部分 · Context 放在哪一层Yes
一个 action 改两个 state第 1 部分 · 一次更新两个 stateYes
四个页面的切换第 2 部分 · 页面状态机Yes
按类型分组 + 六张卡的字段第 2 部分 · 分组渲染与 testid 契约Yes
Loading 一秒后自动跳转第 2 部分 · setTimeout 与清理函数Yes
历史只留最新三条、最新在最上第 2 部分 · slice(-3).reverse()Yes
脚手架缺陷:.js 里写 JSX第 3 部分 · Debug LabYes
两处能更好的写法(面试会问)第 3 部分 · 面试官会追问的两点No
空文件夹里从零重写第 3 部分 · 从零重写Yes
课程Lessons
Cab Booking · 第 1 部分Cab Booking · Part 1
2 节2 lessons
Context 这一层:放在哪、存什么、怎么改The Context layer: where to put it, what to store, how to update it

先把四个测试读清楚,再搭 Context。这一部分的核心是一个位置问题:Provider 到底该包在哪一层 —— 答错这个,App 自己就用不了 Context。Read the four tests carefully first, then build the Context. The main point of this part is a question of position: which level of the tree the Provider should wrap. Get that wrong and App itself cannot read the Context.

Cab Booking · 第 2 部分Cab Booking · Part 2
4 节4 lessons
四个页面串起来:状态机、分组、一秒延迟Connecting the four pages: the state machine, grouping, and the one-second delay

没有路由,一个 currentPage state 管四个页面。这一部分把页面写出来,顺便撞上两个老考点:effect 的清理函数、和「取最新三条」的数组操作。No router: a single currentPage state controls four pages. This part builds the pages, and it also covers two points that come up again and again: the cleanup function of an effect, and the array operation that takes the three newest items.

Cab Booking · 第 3 部分Cab Booking · Part 3
2 节2 lessons
验收:脚手架的坑、面试追问、从零重写Checking your work: the defect in the provided project, interview follow-up questions, and rewriting in an empty folder

三件事:修掉那个让「完整答案」原样跑不起来的缺陷;把源项目两处「测试能过但面试会被问」的写法说清楚;然后在空文件夹里重写一遍。Three things: fix the defect that stops the complete answer from running as given; explain two places in the source project where the code passes the tests but an interviewer would still ask about it; then write the whole app again in an empty folder.