React API
The React API provides components and hooks for integrating Netup recommendations into your React application.
Import
import {
PublishingZone,
PublishingZoneComponent,
PublishingZoneTypeEnum,
NetupRecommendation,
NetupRecommendationProps,
NetupProvider,
useNetup,
} from "@front-commerce/netup/react";
Components
NetupProvider
Context provider that manages Netup state and provides recommendation data to child components.
Props
interface NetupProviderProps {
children: React.ReactNode;
}
Usage
import { NetupProvider } from "@front-commerce/netup/react";
export default function App() {
return <NetupProvider>{/* Your app components */}</NetupProvider>;
}
Behavior
- State Management: Maintains recommendations received from Netup
- Event Listening: Automatically listens for recommendation events
- Disabled State: Provides warning functions when Netup is disabled
- Data Merging: Merges new recommendations with existing ones, replacing duplicates by zone ID
PublishingZone
Component that displays recommendations for a specific zone type.
Props
interface PublishingZoneProps {
type: PublishingZoneTypeEnum;
RecommendationComponent?: React.ComponentType<NetupRecommendationProps>;
}
Parameters
type: PublishingZoneTypeEnum
Required. The type of publishing zone to display recommendations for.
See PublishingZoneTypeEnum
for available values.
RecommendationComponent?: React.ComponentType<NetupRecommendationProps>
Optional. Custom component to render each recommendation. If not provided, uses the default recommendation component.
Usage
Basic Usage:
import {
PublishingZone,
PublishingZoneTypeEnum,
} from "@front-commerce/netup/react";
function HomePage() {
return (
<div>
<h1>Welcome</h1>
<PublishingZone type={PublishingZoneTypeEnum.homePage} />
</div>
);
}
With Custom Component:
import {
PublishingZone,
PublishingZoneTypeEnum,
NetupRecommendationProps,
} from "@front-commerce/netup/react";
function CustomRecommendation({ recommendation }: NetupRecommendationProps) {
return (
<div className="custom-rec">
<h3>{recommendation.json.catchphrase}</h3>
<div className="products">
{recommendation.json.products.map((sku) => (
<ProductCard key={sku} sku={sku} />
))}
</div>
</div>
);
}
function ProductPage() {
return (
<div>
<PublishingZone
type={PublishingZoneTypeEnum.productPage}
RecommendationComponent={CustomRecommendation}
/>
</div>
);
}
Behavior
- Zone Filtering: Only displays recommendations matching the specified type
- Configuration Check: Returns
null
if Netup is disabled - Empty State: Returns
null
if no recommendations are available for the zone type - Multiple Zones: Handles multiple publishing zones of the same type
- DOM Attributes: Adds
data-wsb-used="true"
attribute for Netup integration
Hooks
useNetup
Hook to access the Netup context and recommendation data.
Signature
function useNetup(): NetupContextValue;
Returns
interface NetupContextValue {
recommendations: NetupRecommendation[];
getRecommendationsForZone: (zoneId: string) => NetupRecommendation | null;
getRecommendationsForZones: (zoneIds: string[]) => NetupRecommendation[];
}
Usage
import { useNetup } from "@front-commerce/netup/react";
function RecommendationStats() {
const { recommendations, getRecommendationsForZone } = useNetup();
return (
<div>
<p>Total recommendations: {recommendations.length}</p>
<button
onClick={() => {
const homeRec = getRecommendationsForZone("ZH1");
console.log("Home recommendations:", homeRec);
}}
>
Check Home Zone
</button>
</div>
);
}
Context Methods
getRecommendationsForZone(zoneId: string)
Returns a single recommendation for the specified zone ID.
Parameters:
zoneId: string
- The publishing zone ID to search for
Returns: NetupRecommendation | null
const homeRecommendation = getRecommendationsForZone("ZH1");
if (homeRecommendation) {
console.log("Products:", homeRecommendation.json.products);
}
getRecommendationsForZones(zoneIds: string[])
Returns recommendations for multiple zone IDs.
Parameters:
zoneIds: string[]
- Array of zone IDs to search for
Returns: NetupRecommendation[]
const productRecommendations = getRecommendationsForZones(["ZP1", "ZP2"]);
console.log(`Found ${productRecommendations.length} product recommendations`);
Types and Enums
PublishingZoneTypeEnum
Enum defining the types of publishing zones supported by Netup.
enum PublishingZoneTypeEnum {
productPage = "productPage",
homePage = "homePage",
cartPage = "cartPage",
productAddedPopInCrosssells = "productAddedPopInCrosssells",
categoryPage = "categoryPage",
}
Values
Value | Description | Usage |
---|---|---|
productPage | Product detail page recommendations | Related products, upsells |
homePage | Homepage recommendations | Featured products, trending items |
cartPage | Shopping cart recommendations | Add-on products, alternatives |
productAddedPopInCrosssells | Cross-sell popup after adding to cart | Complementary products |
categoryPage | Category page recommendations | Related categories, featured products |
NetupRecommendation
Type representing a recommendation from Netup.
interface NetupRecommendation {
publishingZoneId: string;
json: {
catchphrase?: string;
products: string[];
};
}
Properties
publishingZoneId: string
The unique identifier for the publishing zone this recommendation belongs to.
json.catchphrase?: string
Optional marketing message or title for the recommendation.
json.products: string[]
Array of product SKUs recommended for this zone.
NetupRecommendationProps
Props interface for custom recommendation components.
interface NetupRecommendationProps {
recommendation: NetupRecommendation;
}
Usage
function MyRecommendation({ recommendation }: NetupRecommendationProps) {
const { publishingZoneId, json } = recommendation;
return (
<section data-zone={publishingZoneId}>
{json.catchphrase && <h2>{json.catchphrase}</h2>}
<ProductGrid skus={json.products} />
</section>
);
}