Loading…
Loading…
Quick setup guide for installing and configuring @woographql/react-hooks for headless WooCommerce development.
Quick setup guide for @woographql/react-hooks - a React hook library for headless WooCommerce storefronts powered by GraphQL for WooCommerce.
Note: If you're using
@woographql/next, most of this setup is handled automatically. The session provider, token manager, and operations are pre-configured based on your project settings.
This package is hosted on a private registry. Access requires a valid WooGraphQL Pro license.
Create or update .npmrc in your project root:
registry=https://registry.npmjs.org/ @woographql:registry=https://yeetsquad.net/ //yeetsquad.net/:_authToken="YOUR_LICENSE_KEY" always-auth=true
Replace YOUR_LICENSE_KEY with your WooGraphQL Pro license key.
npm install @woographql/react-hooks @woographql/session-utils
// SessionProvider.tsx import { PropsWithChildren, Context } from 'react'; import { useContext } from 'react'; import { useAsyncSessionManager, createSessionContext, SessionData, SessionOperations, } from '@woographql/react-hooks'; import { AsyncTokenManagerInterface } from '@woographql/session-utils'; export interface AppSessionData extends SessionData<AsyncTokenManagerInterface> {} export const sessionContext = createSessionContext<AppSessionData, AsyncTokenManagerInterface>(); export const useSession = () => useContext(sessionContext); export function SessionProvider({ children, tokenManager, operations, }: PropsWithChildren<{ tokenManager: AsyncTokenManagerInterface; operations: SessionOperations<AppSessionData>; }>) { const store = useAsyncSessionManager<AppSessionData>( tokenManager, operations, { startOnMount: true } ); const Provider = sessionContext.Provider as Context<AppSessionData>['Provider']; return <Provider value={store}>{children}</Provider>; }
import { SessionProvider } from './SessionProvider'; export function App({ children }) { return ( <SessionProvider tokenManager={tokenManager} operations={operations}> {children} </SessionProvider> ); }
'use client'; import { useSession } from './SessionProvider'; export function Header() { const { customer, cart, isAuthenticated, logout, isExecuting } = useSession(); return ( <header> <a href="/cart">Cart ({cart?.contents?.itemCount ?? 0})</a> {isAuthenticated ? ( <button onClick={() => logout()} disabled={isExecuting()}> Logout </button> ) : ( <a href="/login">Login</a> )} </header> ); }
This library includes built-in support for WooGraphQL Pro features:
addBundleToCart mutationaddCompositeToCart mutationSee useCartMutations for detailed examples.