Skip to content

Latest commit

 

History

History
executable file
·
117 lines (86 loc) · 3.77 KB

README.md

File metadata and controls

executable file
·
117 lines (86 loc) · 3.77 KB

NPM version NPM downloads NPM license Codecov Travis Bundle size

About

Currency converter React hook

Demo

Playground – play with the library in CodeSandbox

Similar Projects

How to Install

First, install the library in your project by npm:

$ npm install react-currency-hooks

Or Yarn:

$ yarn add react-currency-hooks

Getting Started

• Import hook in React application file:

import { useCurrency } from 'react-currency-hooks';

Params

Name Type Default Description
amount number Amount of money to convert
options {} Convertion options

Options

Name Type Default Description
from string Currency to be converted
to string or string[] The currency to which it is converted
base string Base currency
rates Rates {} Currency rates
keepPrecision boolean true true (return exact values), false (return values rounded to 2 places)

Returned Values

Type Description
number or object with currencies passed in to Converted value

Example

import React from 'react';
import { useCurrency } from 'react-currency-hooks';

const App = () => {
  const rates = {
    GBP: 0.92,
    EUR: 1.0,
    CHF: 1.08,
    USD: 1.12,
  };

  /*
   * 1. With single `to` value
   */
  const currency = useCurrency(200, {
    from: 'USD',
    to: 'CHF',
    base: 'EUR',
    rates,
  });

  return <p>USD to CHF: {currency}</p>;

  /*
   * 2. With multiple `to` values
   */
  const { chf, gbp } = useCurrency(200, {
    from: 'USD',
    to: ['CHF', 'GBP'],
    base: 'EUR',
    rates,
  });

  return (
    <>
      <p>USD to CHF: {chf}</p>
      <p>USD to GBP: {gbp}</p>
    </>
  );
};

export default App;

License

This project is licensed under the MIT License © 2020-present Jakub Biesiada