# Hydrogen React
Hydrogen React is a performant, framework-agnostic library of React components, reusable functions, and utilities for interacting with Shopify’s [Storefront API](https://shopify.dev/docs/api/storefront). It’s bundled with [Hydrogen](https://shopify.dev/docs/api/hydrogen), but can be used by any React-based web app.
## Setup
1. Install Hydrogen React in your project with your preferred package manager.
1. Import components, hooks, or utilities that you want to use in your app. For more detailed instructions, see the Getting Started guide.
```
npm i --save @shopify/hydrogen-react
```
```
yarn add @shopify/hydrogen-react
```
- [Tutorial](https://shopify.dev/docs/custom-storefronts/hydrogen-react): Getting Started with Hydrogen React
## Authentication
To use Hydrogen React, you need to authenticate with and make requests to the [Storefront API](https://shopify.dev/docs/api/storefront). Hydrogen React includes an [API client](https://shopify.dev/docs/api/hydrogen-react/2025-01/utilities/createstorefrontclient) to securely handle API queries and mutations.
You can create and manage Storefront API access tokens by installing the [Headless sales channel](https://apps.shopify.com/headless) on your store.
Apps have access to [two kinds of tokens](https://shopify.dev/docs/api/usage/authentication#access-tokens-for-the-storefront-api): a public API token, which can be used in client-side code, and a private API token, which should only be used in server-side contexts and never exposed publicly.
```javascript
import {createStorefrontClient} from '@shopify/hydrogen-react';
export const client = createStorefrontClient({
// load environment variables according to your framework and runtime
storeDomain: process.env.PUBLIC_STORE_DOMAIN,
publicStorefrontToken: process.env.PUBLIC_STOREFRONT_API_TOKEN,
});
```
```
# Replace with your own store domain and Storefront API token
PUBLIC_STOREFRONT_API_TOKEN="public_token"
PRIVATE_STOREFRONT_API_TOKEN="private_token"
PUBLIC_STORE_DOMAIN="store_id.myshopify.com"
```
```javascript
import {client} from './client.js';
export async function getServerSideProps() {
const response = await fetch(client.getStorefrontApiUrl(), {
body: JSON.stringify({
query: GRAPHQL_QUERY,
}),
// Generate the headers using the private token.
headers: client.getPrivateTokenHeaders(),
method: 'POST',
});
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
return {props: json};
}
const GRAPHQL_QUERY = `
query {
shop {
name
}
}
`;
```
- [Install](https://apps.shopify.com/headless): Headless sales channel
## Versioning
Hydrogen React is tied to specific versions of the [Storefront API](https://shopify.dev/docs/api/storefront), which is versioned quarterly. For example, if you're using Storefront API version `2023-10`, then Hydrogen versions `2023.10.x` are fully compatible.
> Caution:
>If a Storefront API version includes breaking changes, then the corresponding Hydrogen React version will include the same breaking changes.
- [Learn more](https://shopify.dev/docs/api/usage/versioning): Shopify API versioning
- [Learn more](https://shopify.dev/changelog): Developer changelog
## Components
Components include all the business logic and data parsing required to produce predictable markup for objects available through the Storefront API. Components provide defaults but can be customized. Hydrogen React components include no visual styles, other than the ones provided natively by browsers.
```javascript
import {ShopPayButton} from '@shopify/hydrogen-react';
export function MyProduct({variantId}) {
return ;
}
```
## Hooks
Hooks are functions that provide reusable business and/or state logic. They give you additional flexibility to control the behavior and display of Hydrogen React components.
```javascript
import {useMoney} from '@shopify/hydrogen-react';
export function MyComponent({variant}) {
const {currencyCode, currencySymbol, amount} = useMoney(variant.pricev2);
return (
{currencyCode}
{currencySymbol}
{amount}
);
}
```
## Utilities
Utilities are reusable functions for common manipulations performed on data returned from the Storefront API.
```javascript
import {flattenConnection, MediaFile} from '@shopify/hydrogen-react';
export function Product({product}) {
const media = flattenConnection(product.media);
return (
<>
{media.map((mediaFile) => {
return ;
})}
>
);
}
```
## How Hydrogen React works with Hydrogen
Hydrogen React is bundled as part of Hydrogen, Shopify’s opinionated headless commerce stack built on [Remix](https://remix.run). Hydrogen React is also published separately as a standalone package so that it can be used by other React-based frameworks.
Hydrogen adds features like standard routes, caching strategies, redirects, and SEO. When using Hydrogen, you can also install the Hydrogen sales channel, which includes built-in support for Oxygen, Shopify’s global edge hosting platform. Consider which approach works best for your use case and existing technology stack.
- [Learn more](https://shopify.dev/docs/custom-storefronts/hydrogen): Hydrogen
- [Install](https://apps.shopify.com/hydrogen): Hydrogen sales channel
## References
- [AddToCartButton](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/addtocartbutton.txt): The `AddToCartButton` component renders a button that adds an item to the cart when pressed. It must be a descendent of the `CartProvider` component.
- [BuyNowButton](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/buynowbutton.txt): The `BuyNowButton` component renders a button that adds an item to the cart and redirects the customer to checkout. Must be a child of a `CartProvider` component.
- [CartCheckoutButton](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/cartcheckoutbutton.txt): The `CartCheckoutButton` component renders a button that redirects to the checkout URL for the cart. Must be a descendent of a `CartProvider` component.
- [CartCost](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/cartcost.txt): The `CartCost` component renders a `Money` component with the cost associated with the `amountType` prop. If no `amountType` prop is specified, then it defaults to `totalAmount`. Depends on `useCart()` and must be a child of ``
- [CartLinePrice](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/cartlineprice.txt): `@deprecated` Use `Money` instead. To migrate, use the `priceType` prop that matches the corresponding property on the `CartLine` object: - `regular`: `cartLine.cost.totalAmount` - `compareAt`: `cartLine.cost.compareAtAmountPerQuantity` For example Before: `` After: `` The `CartLinePrice` component renders a `Money` component for the cart line merchandise's price or compare at price.
- [CartLineProvider](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/cartlineprovider.txt): The `CartLineProvider` component creates a context for using a cart line.
- [CartLineQuantity](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/cartlinequantity.txt): The `` component renders a `span` (or another element / component that can be customized by the `as` prop) with the cart line's quantity. It must be a descendent of a `` component, and uses the `useCartLine()` hook internally.
- [CartLineQuantityAdjustButton](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/cartlinequantityadjustbutton.txt): The `` component renders a `span` (or another element / component that can be customized by the `as` prop) with the cart line's quantity. It must be a descendent of a `` component, and uses the `useCartLine()` hook internally.
- [CartProvider](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/cartprovider.txt): The `CartProvider` component synchronizes the state of the Storefront API Cart and a customer's cart, and allows you to more easily manipulate the cart by adding, removing, and updating it. It could be placed at the root of your app so that your whole app is able to use the `useCart()` hook anywhere. There are props that trigger when a call to the Storefront API is made, such as `onLineAdd={}` when a line is added to the cart. There are also props that trigger when a call to the Storefront API is completed, such as `onLineAddComplete={}` when the fetch request for adding a line to the cart completes. The `CartProvider` component must be a descendant of the `ShopifyProvider` component .
- [ExternalVideo](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/externalvideo.txt): The `ExternalVideo` component renders an embedded video for the Storefront API's [ExternalVideo object](https://shopify.dev/api/storefront/reference/products/externalvideo).
- [Image](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/image.txt): The `Image` component renders an image for the Storefront API's [Image object](https://shopify.dev/api/storefront/reference/common-objects/image) by using the `data` prop. You can [customize this component](https://shopify.dev/api/hydrogen/components#customizing-hydrogen-components) using passthrough props. An image's width and height are determined using the following priority list: 1. The width and height values for the `loaderOptions` prop 2. The width and height values for bare props 3. The width and height values for the `data` prop If only one of `width` or `height` are defined, then the other will attempt to be calculated based on the image's aspect ratio, provided that both `data.width` and `data.height` are available. If `data.width` and `data.height` aren't available, then the aspect ratio cannot be determined and the missing value will remain as `null`
- [MediaFile](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/mediafile.txt): The `MediaFile` component renders the media for the Storefront API's [Media object](https://shopify.dev/api/storefront/reference/products/media). It renders an `Image`, `Video`, an `ExternalVideo`, or a `ModelViewer` depending on the `__typename` of the `data` prop.
- [ModelViewer](https://shopify.dev/docs/api/hydrogen-react/2023-01/components/modelviewer.txt): The `ModelViewer` component renders a 3D model (with the `model-viewer` custom element) for the Storefront API's [Model3d object](https://shopify.dev/api/storefront/reference/products/model3d). The `model-viewer` custom element is lazily downloaded through a dynamically-injected `