Code Monkey home page Code Monkey logo

react-dropify's Introduction

React Dropify

Let's drop some cool stuff ๐Ÿ”ฅ

react

๐Ÿšง Still in alpha โ€” API might change with new releases ๐Ÿšง


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

react-dropify's People

Contributors

dependabot[bot] avatar julianbenegas avatar moransantiago avatar ragojose avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.