Currency converter React hook
Playground – play with the library in CodeSandbox
First, install the library in your project by npm:
$ npm install react-currency-hooks
Or Yarn:
$ yarn add react-currency-hooks
• Import hook in React application file:
import { useCurrency } from 'react-currency-hooks';
Name | Type | Default | Description |
---|---|---|---|
amount | number | |
Amount of money to convert |
options | {} | |
Convertion 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) |
Type | Description |
---|---|
number or object with currencies passed in to |
Converted value |
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;
This project is licensed under the MIT License © 2020-present Jakub Biesiada