1.3.0 • Published 1 month ago

@storecraft/sdk-react-hooks v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Storecraft Official React Hooks

Official React hooks for storecraft's javascript SDK. Many hooks manage the state into the browser LocalStorage and even a cache hook for IndexDB. All hooks have types and documentation.

Start by installing the package:

npm i @storecraft/sdk-react-hooks

Some of the useful hooks are:

useStorecraft()

Gain access into the SDK, you can initialize with sdk config or you can defer it and use the updateConfig callback. Either way, the config will be saved locally with LocalStorage at the browser.

import { useStorecraftSDK } from '@storecraft/sdk-react-hooks'

const {
  config,
  sdk, 
  isAuthenticated, 
  error,
  actions: {
    updateConfig
  }
} = useStorecraftSDK(config)

useAuth()

Gain access and subscribe to authentication updates

import { useAuth } from '@storecraft/sdk-react-hooks'

const {
  auth,
  isAuthenticated,
  actions: {
    signin, 
    signup, 
    signout
  }
} = useAuth();

useDocument()

Fetch / Mutate a single document by id or handle

import { useDocument } from '@storecraft/sdk-react-hooks'

const {
  doc, 
  sdk,
  loading, 
  hasLoaded, 
  error, 
  op, 
  resource,
  document,
  actions: { 
    reload, upsert, remove, setError,
  }
} = useDocument(
  'products', 
  'product_id_or_handle', 
  autoLoad=true, 
  try_cache_on_autoload=true
);

useCollection()

Query collection of documents with pagination

import { useCollection } from '@storecraft/sdk-react-hooks'

const {
  pages, 
  page,
  loading, 
  error, 
  sdk,
  queryCount, 
  resource,
  actions: {
    prev, 
    next, 
    query,
    poll, 
    removeDocument
  },
} = useCollection(
  'products', 
  { // optional initial query
    order: 'desc',
    sortBy: ['updated_at', 'id'],
    vql: {
      updated_at: {
        $gte: '2024-03-24',
      },
      active: true,
      $or: [
        { $search: 'tag:genre_action' },
        { $search: 'tag:genre_sports' },
      ],
    },
    limit: 10,
  },
  autoLoad=true, 
);

useCart()

A utility hook to manage a frontend cart with LocalStorage support. This can be used later to create a checkout session.

import { useCart } from '@storecraft/sdk-react-hooks'

const {
  cart: {
    line_items, 
    coupons, 
    shipping,
    customer,
    updated_at,
    created_at,
  },
  actions: {
    addItem, 
    removeItem, 
    clearCart,
    setCoupons,
    setShipping,
    setCustomer,
  }
} = useCart();
Author: Tomer Shalev (tomer.shalev@gmail.com)
1.0.38

2 months ago

1.2.5

2 months ago

1.3.0

1 month ago

1.0.37

3 months ago

1.0.36

3 months ago

1.0.35

3 months ago

1.0.34

3 months ago

1.0.33

4 months ago

1.0.32

4 months ago

1.0.29

5 months ago

1.0.28

6 months ago

1.0.27

8 months ago

1.0.31

4 months ago

1.0.30

5 months ago

1.0.2

9 months ago

1.0.26

9 months ago

1.0.3

9 months ago

1.0.1

9 months ago

0.1.0

1 year ago