Skip to content
Tutorials Center
My learning

Topics

Lesson 114 of 132 ยท Projects

Project: Currency Converter

Build a currency converter in React in three stages - controlled inputs, derived results from a rates table, and Intl formatting with a swap button and a simulated live-rates load.

A currency converter looks like a toy, but it teaches one of the most useful habits in React: do not store what you can calculate. The amount, the "from" currency and the "to" currency are state. The converted result is not state at all; it is computed on every render from those three values and a table of rates.

You will also meet Intl.NumberFormat, the browser's built-in formatter, which handles symbols, decimals and grouping for every locale without a library. If controlled inputs or derived data feel unfamiliar, skim forms and inputs and choosing state structure first.

The exchange rates in this lesson are fixed example numbers, not real market data, and the "live" load in stage 3 is a fake request built with setTimeout. The sandbox cannot call real rate APIs, and a tutorial should not pretend to give financial figures.

Stage 1: inputs and a rates table

Store every rate relative to one base currency (here USD). To convert between any two currencies, go through the base: divide by the source rate, multiply by the target rate. One table then supports every pair, instead of N x N entries.

Stage 1: derived result

The amount is kept as a string, because that is what the input gives you and because '' (an empty field) is a legitimate in-between state. Converting to a number happens only when calculating. Try clearing the field: the result becomes 0.00 instead of crashing.

Stage 2: format with Intl and add a swap

toFixed(2) is wrong for yen (no decimals) and gives no symbol. Intl.NumberFormat knows each currency's rules. Create the formatter from the currency code; the browser does the rest.

Stage 2: Intl formatting and swap

Notice valid: validation is also derived. There is no error state to keep in sync. The aria-live region lets screen readers announce the new result.

Stage 3: loading rates (simulated)

Real converters fetch rates. We keep the same loading/ready structure as in data fetching but use a fake function so the lesson runs anywhere. The effect cleans up with an ignore flag so a late response cannot overwrite newer state (effect cleanup).

Stage 3: simulated rates load

Changing the base shows "Loading rates..." again, because setRates(null) runs at the start of each effect. Switch quickly several times: only the last request is allowed to update the screen.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the converted result not stored in state?

    Anything computable from existing state should be computed during render. A stored copy must be updated in every handler and can easily become stale.
  2. 2. Why keep the amount as a string in state?

    Input values are strings. Keeping the raw text lets the user clear the box and type freely; you convert to a number only when you calculate.
  3. 3. What does Intl.NumberFormat with style 'currency' give you over toFixed(2)?

    Intl formats according to locale and currency rules (yen has no decimals, for instance). It does not know exchange rates.
  4. 4. What does the ignore flag in the effect cleanup protect against?

    When the base changes, the cleanup marks the earlier request as stale, so its late response is ignored.

Summary

  • Store inputs; derive results, validity and formatted text during render.
  • Keep one rates table relative to a base currency and convert through it.
  • Intl.NumberFormat formats currencies correctly for any locale.
  • A fetch effect needs loading state and a cleanup flag against stale responses.
  • Rates here are made-up examples and the load is simulated.

Next: Project: Movie Search App