react-shopify-drop is a react library for interacting with shopify's storefront api.
This is a private package from basement.studio.
Use the package manager yarn to install react-shopify-drop.
yarn add react-shopify-drop
import {
createStorefrontClient,
StorefrontProvider
} from 'react-shopify-drop';
export const storefrontClient = createStorefrontClient({
accessToken: 'storefront-app-access-token',
endpoint: 'storefront-app-graphql-endpoint',
});
const App = () => (
<StorefrontProvider
appCartId="app-name-cart-id"
client={storefrontClient}
>
<MyApp />
</StorefrontProvider>
);
export default App;
Under the hood, the <StorefrontProvider />
creates a friendly API with a set of useful properties to interact with your shopify application
import { useStorefront } from 'react-shopify-drop';
const Component = () => {
const api = useStorefront();
return <div />;
};
The API comes in form of an object with the following properties:
An object which contains the shopify users cart.
The amount of items in the current cart.
A toggle state to help manage the cart UI state (opened / closed).
- isOn: boolean - Defines the cart UI state.
- handleToggle: function - Toggles the cart UI state.
- handleOn: function - Sets the cart UI state as on (opened).
- handleOff: function - Sets the cart UI state as on (closed).
An object containing a set of possible errors
- createCartError: Error | null
- addLineItemError: Error | null
- updateLineItemError: Error | null
- removeLineItemError: Error | null
Adds an item to the shopify cart.
Updates an item inside the shopify cart.
Removes an item from the shopify cart.
In the other hand, you can also export the created client and import it outside your application frontend (f.e: next.js page getStaticProps function)
import { storefrontClient } from '~/pages/_app.js';
export const getStaticProps = async () => {
const { product } = await storefrontClient.GetProductByHandle({ handle: 'my-product' });
return {
props: {
product
},
revalidate: 1
};
};
CreateCart
: function (variables?: CreateCartMutationVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<CreateCartMutation>
Creates a new cart.
CreateCartWithLines
: function (variables: CreateCartWithLinesMutationVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<CreateCartWithLinesMutation>
Creates a new cart with line.
AddLineItem
: function (variables: AddLineItemMutationVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<AddLineItemMutation>
Adds an item to a line.
UpdateLineItem
: function (variables: UpdateLineItemMutationVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<UpdateLineItemMutation>
Updates a line.
RemoveLineItem
: function (variables: RemoveLineItemMutationVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<RemoveLineItemMutation>
Removes an item from a line.
FetchCart
: function (variables: FetchCartQueryVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<FetchCartQuery>
Fetches an already created cart.
GetProductByHandle
: function (variables: GetProductByHandleQueryVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<GetProductByHandleQuery>
Fetches a product based on a handle.
GetAllProducts
: function (variables?: GetAllProductsQueryVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<GetAllProductsQuery>
Fetches all products on a shop.
GetProductsOnCollection
: function (variables: GetProductsOnCollectionQueryVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<GetProductsOnCollectionQuery>
Fetches all collection products on a shop.
GetCollections
: function (variables?: GetCollectionsQueryVariables, requestHeaders?: Dom.RequestInit["headers"]) => Promise<GetCollectionsQuery>
Fetches all collections.
Executes a custom query.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.