Skip to content

cocart-headless/cocart-react-native

Repository files navigation

@cocart/react-native-sdk

React Native npm version Tests License

Official React Native SDK for the CoCart REST API. Build headless WooCommerce storefronts in React Native using simple hooks.

Note

This SDK is a thin wrapper over @cocartheadless/sdk. It does not provide pre-built UI screens or native components — it provides state and data via hooks that you wire into your own screens using standard React Native primitives.

Important

This SDK is still in development and not yet ready for production use. Provide feedback if you experience a bug.

TODO to complete the SDK

  • Add SDK docs to documentation site
  • Add support for Cart API extras
  • Add Checkout API support

How It Works

It adds three things on top of the core SDK:

  • Storage adaptersMMKVStorage and AsyncStorageAdapter so cart sessions and tokens persist across app restarts
  • CoCartProvider — constructs the client, restores the session on mount and on app foreground
  • HooksuseCart, useCartMutations, useProducts, useAccount for idiomatic React Native usage

All core logic (authentication, cart, products, sessions, JWT, 2FA, account, error handling) is inherited from the TypeScript SDK automatically.


Requirements

Support Policy

See SUPPORT.md for our versioning policy and support lifecycle.


Installation

npm install @cocart/react-native-sdk @cocartheadless/sdk

Optional: Native Storage

For persistent cart sessions (recommended):

MMKV (fast synchronous storage — requires native build):

npm install react-native-mmkv
cd ios && pod install

AsyncStorage (async, Expo managed workflow compatible):

npm install @react-native-async-storage/async-storage

If neither is installed, sessions are kept in memory only and lost on app restart.


Quick Start

Wrap your app in CoCartProvider, then use hooks in any component:

import React from 'react';
import { CoCartProvider, MMKVStorage } from '@cocart/react-native-sdk';
import { StorefrontScreen } from './screens/StorefrontScreen';

export default function App() {
  return (
    <CoCartProvider
      siteURL="https://your-store.com"
      options={{ storage: new MMKVStorage() }}
    >
      <StorefrontScreen />
    </CoCartProvider>
  );
}
import React from 'react';
import { View, Text, Button, FlatList } from 'react-native';
import { useCart, useCartMutations, useProducts } from '@cocart/react-native-sdk';

export function StorefrontScreen() {
  const { products, loading: productsLoading } = useProducts();
  const { cart, loading: cartLoading } = useCart();
  const { addItem } = useCartMutations();

  if (productsLoading || cartLoading) return <Text>Loading...</Text>;

  return (
    <View>
      <FlatList
        data={products?.toObject() as any[]}
        keyExtractor={(item) => String(item.id)}
        renderItem={({ item }) => (
          <View>
            <Text>{item.name}</Text>
            <Button title="Add to Cart" onPress={() => addItem(item.id, 1)} />
          </View>
        )}
      />
      <Text>Cart items: {cart?.getItemCount() ?? 0}</Text>
    </View>
  );
}

Documentation

Guide Description
Configuration & Setup Provider options, storage adapters, session restore
Authentication Guest, Basic Auth, JWT, 2FA
Cart API Add, update, remove items, coupons, shipping
Products API Browse, filter, categories, tags
Account API Profile, orders, downloads, reviews
Error Handling Error hierarchy, catching errors, 2FA flow
Hooks Reference useCart, useCartMutations, useProducts, useAccount, useCoCart
Utilities Currency formatter, timezone helper

CoCart Channels

Credits

CoCart is developed and maintained by CoCart Headless, LLC.

License

Released under the MIT License.

About

Official CoCart SDK for React Native

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors