Coder Social home page Coder Social logo

use-snipcart's Introduction

๐Ÿ›’ useSnipcart

A React hook for checking in to Snipcart state.

๐Ÿš€ Getting Started

Note: useSnipcart requires the standard installation from Snipcart including loading the Snipcart script with your unique API key

Install useSnipcart:

yarn add use-snipcart
# or
npm install use-snipcart

Wrap the application or parent with the Snipcart Provider to provide globally accessible state:

import { SnipcartProvider } from 'use-snipcart';

<SnipcartProvider>
...
</SnipcartProvider>

Use the hook to access state:

import { useSnipcart } from 'use-snipcart';

const { cart = {} } = useSnipcart();

๐Ÿง What's inside?

useSnipcart uses the Snipcart Store API to subscribe to the local instance of the Snipcart store to sync that store up with React state.

It then passes that state through the hook to make it available to the application.

For instance, a common use case is grabbing the current subtotal of the cart, which can be found using:

const { cart = {} } = useSnipcart();
const { subtotal = '0.00' } = cart;

To see what all is included, check out the SnipcartState documentation:

https://docs.snipcart.com/v3/sdk/reference#core-state-SnipcartState

๐Ÿค” Why useSnipcart?

Using UI frameworks like React give developers the ability to easily provide clientside route changes, which due to how the DOM loads, doesn't lend itself well to loading the Snipcart script potentially multiple time, initializing that multiple times, and / or keeping that all in sync.

This takes advantage of Snipcart's API that attaches to the window, where we can subscribe to Snipcart independently, and make sure we're always using the correct version when updating our application.

use-snipcart's People

Contributors

colbyfayock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

okslutsiv

use-snipcart's Issues

Is use-snipcart 1.1.3 Outdated?

I need to update my npm packages. use-snipcart didn't let me to update any package that depends on react. (React 18)

Also installing use-snipcart on up to date next.js project, gives me resolve errors. see picture attached.

Please update the package to work with react 18 or help with a solution. Appreciate hugely

CleanShot 2022-09-06 at 11 42 38@2x

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.