Skip to main content
Frontend tutorial

7. Fetch

Until now ShippingBoard imported its customers and ramps as JSON files. In our apps the data lives in SAP, and the frontend asks ABAP REST services for it. Normally the API is on the same domain as the web app, so the base URL can be /api/. We don't have SAP here, so we get the same JSON files from GitHub.

We ask for them first with await, then with then. At the end we fix the store, so the user can tell "no ramps" from "no answer".

Lessons​

  1. Fetch using await
  2. Fetch using then
  3. Fixing store
  4. Do it in ShippingBoard

Lessons 7.1–7.3 run here in the browser; 7.4 continues on your computer in shippingboard-lite, branch create-fetch.

07 / Fetch

From RAMPS.json to the board

From RAMPS.json to the boardstore.init sets the status to loading and sends a request for RAMPS.json, written with await or then. If response.ok, runInAction stores the ramps and sets done, and the board shows ramps A, B and more. If not, the request throws, the status becomes error and a Retry button calls init again.RequestRAMPS.jsonawait or thenresponse.okBoardRamp ARamp B…store.statusdone and errorin runInActionloadinginitokdonenot ok:throwerrorRetry

The store loads the data. React only shows what the store has.