0.1.2 ā€¢ Published 12 months ago

react-dropify v0.1.2

Weekly downloads
-
License
-
Repository
github
Last release
12 months ago

React Dropify

Let's drop some cool stuff šŸ”„

react

react-dropify is the best library for interacting with the Shopify Storefront API (SFAPI).

  • āœ… Start quickly with ready-made queries.
  • āœ… Grab the full power of the SFAPI with custom GraphQL queries.
  • āœ… Type safe with a built in, ready-to-go GraphQL x TypeScript codegen.
  • āœ… Cart state saved to localStorage, and provided via React Context.
  • āœ… Drop ready, with hooks to easily create countdowns and newsletter forms.

Hyped already? Let's start.

Getting Started

1. Install it

yarn add react-dropify

2. Create a Custom App on Shopify and grab your Storefront Access Token.

See instructions here. If you find it hard to create the Custom App, please let us know and we can expand on this point.

Save the Storefront Access Token as a public .env variable.

ā‰ļø The Storefront API Access Token can be public. In fact, the API is optimized for being accessed from the client.

3. Get your Store Domain Name.

This is generally something like <your-store>.myshopify.com.

Save the Store Domain Name as a public .env variable.

3. Create ./react-dropify/config.js at the root of your project.

// ./react-dropify/config.js

// Example showing how you'd name your environment variables in Next.js.
// Make sure you follow the convention of your chosen framework.
// And please, delete these comments!

/**
 * @type {import('react-dropify/generate').ReactDropifyConfig}
 */
module.exports = {
  domain: process.env.NEXT_PUBLIC_SHOPIFY_DOMAIN,
  accessToken: process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN
}

ā‰ļø The Storefront API Access Token can be public. In fact, the API is optimized for being accessed from the client.

4. Get your SDK

Simply run

yarn react-dropify generate

Tip:

  // package.json

  "scripts": {
     // your scripts...
    "generate": "yarn react-dropify generate",
  },

Let's see the results!

Congratulations, you now have a type safe SDK to interact with the SFAPI. Open up ./react-dropify/sdk.ts. It should look something like this:

import config from './config'
import { createReactDropifySdk } from './generated'

export const reactDropifySdk = createReactDropifySdk(config)

reactDropifySdk contains some basic queries, such as:

  • _GetAllProducts
  • _GetAllCollections
  • _GetProductByHandle
  • ... etc

What's even cooler is that you can define custom queries in .graphql files, and if you run the codegen again, you'll get all of those queries available (and type safe) on reactDropifySdk šŸ’„

Isn't that amazing?

Using the StorefrontProvider

The StorefrontProvider is a React Context Provider which manages cart state. Wrap it on your App component.

// Example using Next.js

import { AppProps } from 'next/app'
import { StorefrontProvider } from 'react-dropify'
import { reactDropifySdk } from '~/lib/react-dropify/sdk'

const App = ({ Component, pageProps }: AppProps) => {
  return (
    <StorefrontProvider appCartId="<store>-cart-id" client={reactDropifySdk}>
      <Component {...pageProps} />
    </StorefrontProvider>
  )
}

export default App

And then, somewhere in your app:

import { useStorefront } from 'react-dropify'

const Component = () => {
  const {
    cart,
    cartItemsCount,
    cartToggleState,
    onAddLineItem,
    onRemoveLineItem,
    onUpdateLineItem
  } = useStorefront()

  return <div />
}

When you add an item to cart, the provider:

  1. will check on localStorage to see if there's a cart id stored there.
  2. will fetch the cart only when needed, and will cache the result using swr.
  3. will report errors to an event emitter.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Authors

0.1.0

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.0.48

2 years ago

0.0.47

2 years ago

0.0.46

2 years ago

0.0.45

2 years ago

0.0.44

2 years ago

0.0.43

2 years ago

0.0.42

2 years ago

0.0.41

2 years ago

0.0.40

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago