defaultFormatPrice
import { defaultFormatPrice } from '@rudra-js/react';Call defaultFormatPrice to format a product's price the same way the block does.
defaultFormatPrice(product: Product, locale?: string) => stringReference
defaultFormatPrice(product, locale?)
Parameters
product: The product whosepriceandcurrencyto format.localeoptional: The language tag to format for, such asen-GB. Defaults to the locale of the machine running it.
Returns
The price as text, such as $174.00 for a USD price in en-US.
Caveats
- It throws a
TypeErrorwhenpriceisn't a finite number. Products that passedproductSchemanever have one. - It formats with
Intl.NumberFormat, so each currency gets its own decimal places. AJPYprice of1740shows as¥1,740. - A
localethat isn't a valid language tag, such asen_US, gives the currency code and the plain number, such asUSD 174.
Usage
Formatting a price the same way the block does
Show the price of the product on the page with defaultFormatPrice. Pass the same locale to the block.
import type { ComponentSpec, Product } from '@rudra-js/core';
import { RudraComponent, defaultFormatPrice } from '@rudra-js/react';
const locale = 'en-US';
export function ProductPage({
product,
spec,
products,
}: {
product: Product;
spec: ComponentSpec;
products: Product[];
}) {
return (
<main>
<h1>{product.title}</h1>
<p>{defaultFormatPrice(product, locale)}</p>
<RudraComponent spec={spec} products={products} locale={locale} />
</main>
);
}The price under the heading and the prices in the block read the same.
To add to the block's format, call defaultFormatPrice inside your own formatPrice. Pass it the locale yourself.