react-currency-hooks
TypeScript icon, indicating that this package has built-in type declarations

1.2.3 • Public • Published

react-currency-hooks

NPM version NPM downloads

About

Currency converter React hook

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.00,
    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

Package Sidebar

Install

npm i react-currency-hooks

Weekly Downloads

13

Version

1.2.3

License

MIT

Unpacked Size

8.02 kB

Total Files

13

Last publish

Collaborators

  • jbiesiada