@getspot/spot-widget-react

中文文档
4.8.0发布于 2026/10/30TypeScriptESM + CJS近 30 天下载 7985

React wrapper for Spot Widget

原始使用文档
文档目录19 节
  1. Key Types
  2. Installation
  3. Quick Start
  4. TypeScript Support
  5. Props Reference
  6. Required Props
  7. Optional Props
  8. Callback Props
  9. Using Refs
  10. Ref Methods
  11. API Configuration
  12. Quote Request Data
  13. Single Quote Format
  14. Batch Quote Format
  15. Styling
  16. Error Handling
  17. Compatibility
  18. License
  19. Support

@getspot/spot-widget-react

React component wrapper for the Spot refund guarantee widget.

Note: This React wrapper uses types from @getspot/spot-widget.

Key Types

  • ApiConfig - API configuration (environment, partnerId, customEndpoint)
  • QuoteRequestData - Quote request data for single items or batch requests
  • SelectionData - User selection data returned in callbacks
  • Quote - Quote response data with pricing and terms
  • Theme - Styling customization options

For complete type definitions, see the @getspot/spot-widget documentation.

Installation

npm install @getspot/spot-widget-react

Quick Start

import React from 'react';
import ReactSpotWidget from '@getspot/spot-widget-react';

function App() {
  return (
    <ReactSpotWidget
      apiConfig={{
        environment: 'production', // or 'sandbox' for testing
        partnerId: 'your-partner-id'
      }}
      quoteRequestData={{
        startDate: '2024-01-01T00:00:00Z',
        endDate: '2024-01-07T23:59:59Z',
        currencyCode: 'USD',
        eventType: 'Ski Trip',
        productType: 'Trip',
        productDuration: 'Trip',
        productPrice: 500,
        productId: 'ski-trip-2024',
        cartId: 'cart-123',
        productName: 'Aspen Ski Trip 2024'
      }}
      onOptIn={(data) => {
        console.log('User opted in:', data);
        // Handle opt-in (e.g., add to cart, track analytics)
      }}
      onOptOut={(data) => {
        console.log('User opted out:', data);
        // Handle opt-out (e.g., track analytics)
      }}
      onError={(error) => {
        console.error('Widget error:', error);
        // Handle errors (e.g., show fallback UI)
      }}
    />
  );
}

TypeScript Support

This package includes full TypeScript definitions. All props are typed for better development experience.

Props Reference

Required Props

PropTypeDescription
apiConfigApiConfigConfiguration for the Spot API including environment and partner ID
quoteRequestDataQuoteRequestDataQuote request data containing product and cart information

Optional Props

PropTypeDefaultDescription
showTablebooleantrueWhether to show the payout table
selectionSelectionEnum'unselected'Initial selection state for the widget
themeThemeundefinedTheme customization options for styling the widget

Callback Props

PropTypeDescription
onQuoteRetrieved(quote: Quote) => voidCallback fired when a quote is successfully retrieved
onOptIn(data: SelectionData) => voidCallback fired when user opts in to the refund guarantee
onOptOut(data: SelectionData) => voidCallback fired when user opts out of the refund guarantee
onError(error: ErrorData) => voidCallback fired when an error occurs during quote retrieval
onNoMatchingQuote(data: NoQuoteData) => voidCallback fired when no matching quote is found
onSelectionChange(data: SelectionData) => voidCallback fired when user changes their selection (opt-in or opt-out)

Using Refs

You can access widget methods using React refs:

import React, { useRef } from 'react';
import ReactSpotWidget, { ReactSpotWidgetRef } from '@getspot/spot-widget-react';

function App() {
  const widgetRef = useRef<ReactSpotWidgetRef>(null);

  const handleUpdateQuote = async () => {
    const success = await widgetRef.current?.updateQuote({
      // new quote data
    });
    console.log('Quote updated:', success);
  };

  const handleGetSelection = () => {
    const selection = widgetRef.current?.getSelection();
    console.log('Current selection:', selection);
  };

  const handleValidateSelection = () => {
    const isValid = widgetRef.current?.validateSelection();
    console.log('Selection is valid:', isValid);
  };

  return (
    <div>
      <ReactSpotWidget
        ref={widgetRef}
        // ... props
      />
      <button onClick={handleUpdateQuote}>Update Quote</button>
      <button onClick={handleGetSelection}>Get Selection</button>
      <button onClick={handleValidateSelection}>Validate Selection</button>
    </div>
  );
}

Ref Methods

MethodReturn TypeDescription
updateQuote(data)Promise<boolean>Update the quote with new request data
getSelection()SelectionData | nullGet the current user selection
validateSelection()booleanValidate that the user has made a selection
destroy()voidDestroy the widget instance and clean up resources

API Configuration

The apiConfig prop accepts the following options:

{
  environment: 'production' | 'sandbox' | 'local',
  partnerId: string,
  customEndpoint?: string // Optional custom API endpoint
}

Quote Request Data

The quoteRequestData prop requires the following fields:

Single Quote Format

{
  startDate: string,        // ISO 8601 date string
  endDate: string,          // ISO 8601 date string  
  currencyCode: 'USD' | 'CAD' | 'AUD',
  eventType: string,        // e.g., "Ski Trip", "Concert"
  productType: 'Pass' | 'Trip' | 'Registration',
  productDuration: 'Daily' | 'Seasonal' | 'Trip' | 'Event',
  productPrice: number,     // Price in specified currency
  productId: string,        // Unique product identifier
  cartId: string,           // Cart identifier
  productName: string,      // Human-readable product name
  participantDescription?: string // Optional participant details
}

Batch Quote Format

For multiple items in a cart:

{
  cartId: string,
  cartName: string,
  currencyCode: 'USD' | 'CAD' | 'AUD',
  items: Array<{
    // Same fields as single quote, minus cartId and currencyCode
    cartItemId?: string // Optional unique identifier for cart item
  }>
}

Styling

Customize the widget appearance using the theme prop:

<ReactSpotWidget
  theme={{
    primaryColor: '#007bff',
    borderRadius: '8px',
    fontFamily: 'Arial, sans-serif'
    // Add any CSS custom properties (without -- prefix)
  }}
  // ... other props
/>

Error Handling

Always implement error handling for production use:

<ReactSpotWidget
  onError={(error) => {
    // Log error for debugging
    console.error('Spot Widget Error:', error);
    
    // Show user-friendly message
    toast.error('Unable to load refund options. Please try again.');
    
    // Track error in analytics
    analytics.track('spot_widget_error', {
      message: error.message,
      status: error.status
    });
  }}
  onNoMatchingQuote={() => {
    // Handle case where no quote is available
    console.log('No refund guarantee available for this product');
  }}
  // ... other props
/>

Compatibility

  • React: 18.x
  • TypeScript: 5.x
  • Node.js: 16+

License

See the main package for license information.

Support

For support, please contact [email protected].

版本与发布记录

@getspot/spot-widget-react 共发布 56 个版本,首次发布于 2025/04/26;过去 12 个月发布了 27 个版本。

版本发布日期距今
4.8.0latest2026/10/3017 天前
4.7.02026/09/191 个月前
4.6.02026/08/123 个月前
4.5.02026/08/063 个月前
4.4.22026/08/053 个月前
4.4.12026/07/233 个月前

关于 @getspot/spot-widget-react 的常见问题

@getspot/spot-widget-react 是什么?

React wrapper for Spot Widget

如何安装 @getspot/spot-widget-react?

在项目目录运行 npm install @getspot/spot-widget-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)、react-dom(>=18.0.0)、@types/react(>=18.0.0)、@types/react-dom(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@getspot/spot-widget-react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@getspot/spot-widget-react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@getspot/spot-widget-react 可以商用吗?

@getspot/spot-widget-react 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@getspot/spot-widget-react 的下载量有多少?

根据 npm 官方下载统计,@getspot/spot-widget-react 在 2026/10/17 – 2026/11/15 这 30 天内被下载 7,985 次,日均约 266 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@getspot/spot-widget-react 还在维护吗?

最新版本 4.8.0 发布于 2026/10/30(17 天前);至今共发布 56 个版本,其中过去 12 个月发布了 27 个;首次发布于 2025/04/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。