这是indexloc提供的服务,不要输入任何密码
Skip to main content

Hydrogen React

Hydrogen React is a performant, framework-agnostic library of React components, reusable functions, and utilities for interacting with Shopify’s Storefront API. It’s bundled with Hydrogen, but can be used by any React-based web app.


  1. Install Hydrogen React in your project with your preferred package manager.
  2. Import components, hooks, or utilities that you want to use in your app. For more detailed instructions, see the Getting Started guide.

Install the Hydrogen React package

npm i --save @shopify/hydrogen-react

To use Hydrogen React, you need to authenticate with and make requests to the Storefront API. Hydrogen React includes an API client to securely handle API queries and mutations.

You can create and manage Storefront API access tokens by installing the Headless sales channel on your store.

Apps have access to two kinds of tokens: 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.

Authenticate a Hydrogen app

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,
});

Hydrogen React is tied to specific versions of the Storefront API, 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.

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.

Component example

Component

import {ShopPayButton} from '@shopify/hydrogen-react';

export function MyProduct({variantId}) {
return <ShopPayButton variantIds={[variantId]} />;
}

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.

Hook example

Hook

import {useMoney} from '@shopify/hydrogen-react';

export function MyComponent({variant}) {
const {currencyCode, currencySymbol, amount} = useMoney(variant.pricev2);

return (
<div>
<strong>{currencyCode}</strong>
<span>{currencySymbol}</span>
<span>{amount}</span>
</div>
);
}

Utilities are reusable functions for common manipulations performed on data returned from the Storefront API.

Utility example

Utility

import {flattenConnection, MediaFile} from '@shopify/hydrogen-react';

export function Product({product}) {
const media = flattenConnection(product.media);
return (
<>
{media.map((mediaFile) => {
return <MediaFile data={mediaFile} key={mediaFile.id} />;
})}
</>
);
}

Anchor to with_hydrogenHow Hydrogen React works with Hydrogen

Hydrogen React is bundled as part of Hydrogen, Shopify’s opinionated headless commerce stack built on Remix. 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.