Creating a Currency Converter App with React.js: A Step-by-Step Tutorial

Posted by

Build A Responsive Currency Converter app with React.js

Build A Responsive Currency Converter app with React.js – Tutorial From Scratch

If you’re looking to build a currency converter app with React.js, you’ve come to the right place. In this tutorial, we will walk through the process of creating a responsive currency converter app from scratch using React.js.

Step 1: Set Up Your Development Environment

Before we begin, make sure you have Node.js installed on your computer. You can download and install Node.js from the official website: https://nodejs.org

Step 2: Create a New React App

Open your terminal and run the following command to create a new React app:

npx create-react-app currency-converter

This will create a new directory called currency-converter with all the necessary files and dependencies to start building our app.

Step 3: Install Additional Packages

Next, navigate into the currency-converter directory and install the following additional packages:

cd currency-converter
npm install axios react-bootstrap react-select

Step 4: Build the Currency Converter App

Now it’s time to start building our currency converter app. Create a new file called Converter.js and add the following code:

  {`import React, { useState } from 'react';
  import axios from 'axios';
  import Select from 'react-select';
  import 'bootstrap/dist/css/bootstrap.min.css';

  const Converter = () => {
    const [currencies, setCurrencies] = useState([]);
    const [fromCurrency, setFromCurrency] = useState('USD');
    const [toCurrency, setToCurrency] = useState('EUR');
    const [amount, setAmount] = useState(1);
    const [result, setResult] = useState('');

    const fetchCurrencies = async () => {
      const response = await axios.get('https://api.exchangeratesapi.io/latest');
      const data = Object.keys(response.data.rates);
      const options = data.map((currency) => ({
        label: currency,
        value: currency,
      }));
      setCurrencies(options);
    };

    const handleCurrencyChange = (selectedOption, type) => {
      if (type === 'from') {
        setFromCurrency(selectedOption.value);
      } else {
        setToCurrency(selectedOption.value);
      }
    };

    const convertCurrency = async () => {
      const response = await axios.get(`https://api.exchangeratesapi.io/latest?base=${fromCurrency}`);
      const exchangeRate = response.data.rates[toCurrency];
      const convertedAmount = (amount * exchangeRate).toFixed(2);
      setResult(`${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`);
    };

    useEffect(() => {
      fetchCurrencies();
    }, []);

    return (
      

Currency Converter

handleCurrencyChange(selectedOption, 'from')} /> setAmount(e.target.value)} /> handleCurrencyChange(selectedOption, 'to')} />
{result &&

{result}

}
); }; export default Converter;`}

Save the file and go back to the App.js file. Import and use the Converter component to display the currency converter app on the screen.

  {`import React from 'react';
  import Converter from './Converter';

  function App() {
    return (
      
); } export default App;`}

Step 5: Test and Deploy Your Currency Converter App

Finally, run the following command to start the development server and test your currency converter app:

npm start

Once you’re satisfied with the app, you can deploy it to a hosting platform of your choice.

Congratulations! You’ve successfully built a responsive currency converter app with React.js from scratch. Feel free to customize the app further and add additional features to enhance its functionality.

Written by: Your Name

0 0 votes
Article Rating
13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@evaz2203
11 months ago

Good for beginners. Thank you!! 🙏

@sheeteee
11 months ago

Thanks for this great video!! You just forgot to code the "Swap" button functionality.

@oluwayomioke5862
11 months ago

code: 302

type: "invalid_date"

info: "You have entered an invalid date. [Required format: date=YYYY-MM-DD]"

Got this error for the fetchRates

@fallanstar
11 months ago

nice app

@fallanstar
11 months ago

Nice Project plz Make One Fully Functional Ecommerce Website with Redux Toolkit and Tailwind CSS and plz add payment gateway in this

@niteshprajapat7918
11 months ago

This is so amazing 🔥

@soumeshkhuntia
11 months ago

code: 302

type: "invalid_date"

info: "You have entered an invalid date. [Required format: date=YYYY-MM-DD]"

how to fix this error. this pops up when I check in tanstack/react-query

@filiptrifunovic7763
11 months ago

Hi, thank you for making this tutorial.
At 20:00 mark when u get ur data back, i got nothing.
When i open my console it says
Request failed with status code 500
code "ERR_BAD_RESPONSE"

@dipeshdas3
11 months ago

Keep going man

@manokaranmanivasakan5141
11 months ago

Super. Please make the same application in angular as well

@cevdet-gt3uu
11 months ago

Thanks a lot for sharing.

@rodzadra
11 months ago

Hi, Calvin. Do you know MeteorJS? How about you create something with it? 🙂

@thetalibkhan
11 months ago

Pls add a deployment part as well it's a humble request