React Native theorytheory 0/50 · 0%
Networking · easy

15. Fetching data

fetch, loading and error states for on-chain data.

`fetch` works in React Native just as on the web, commonly used to call a JSON-RPC endpoint or an indexer API for balances and transactions.

async function getBalance(address: string): Promise<number> {
  const res = await fetch(`https://api.example.com/balance/${address}`);
  if (!res.ok) throw new Error("failed to fetch balance");
  const data = await res.json();
  return data.balance;
}

A typical component tracks three states around a fetch: loading, data and error, so the UI can show a spinner, the result, or a retry button appropriately.

Check your understanding

  1. 1. Which built-in function performs HTTP requests in RN?

  2. 2. What three states does a typical data-fetching component track?

  3. 3. How do you check for an HTTP error with fetch?