TIL: RN | 인앱 결제 react-native-iap (6) Hooks - 221130

Lumpen·2022년 11월 30일
0

RN인앱결제

목록 보기
6/16

Hooks

Installation

You first have to wrap your app with the withIAPContext HOC.

import React from 'react';
import {withIAPContext} from 'react-native-iap';

const App = () => <View />;

export default withIAPContext(App);

Usage

use IAP() 후크는 간단한 react-native-iap 메서드에 쉽게 액세스할 수 있는 방법입니다. 이미 컨텍스트를 통해 일부 작업을 수행하여 제품, 구매, 구독, 콜백 및 오류 처리기를 더 빨리 얻을 수 있도록 지원합니다.
아래는 후크를 통해 사용할 수 있는 모든 방법입니다. 나머지 모든 방법(예: requestPurchase)은 'react-native-iap'에서 일반적인 가져오기 {requestPurchase}를 통해 사용할 수 있습니다.

import React from 'react';
import {View, Text} from 'react-native';
import {requestPurchase, useIAP} from 'react-native-iap';

const App = () => {
  const {
    connected,
    products,
    promotedProductsIOS,
    subscriptions,
    purchaseHistories,
    availablePurchases,
    currentPurchase,
    currentPurchaseError,
    initConnectionError,
    finishTransaction,
    getProducts,
    getSubscriptions,
    getAvailablePurchases,
    getPurchaseHistories,
  } = useIAP();

  const handlePurchase = async (sku: string) => {
    await requestPurchase({sku});
  };

  useEffect(() => {
    // ... listen to currentPurchaseError, to check if any error happened
  }, [currentPurchaseError]);

  useEffect(() => {
    // ... listen to currentPurchase, to check if the purchase went through
  }, [currentPurchase]);

  return (
    <>
      <Button
        title="Get the products"
        onPress={getProducts(['com.example.consumable'])}
      />

      {products.map((product) => (
        <View key={product.productId}>
          <Text>{product.productId}</Text>

          <Button
            title="Buy"
            onPress={() => handlePurchase(product.productId)}
          />
        </View>
      ))}
    </>
  );
};

profile
떠돌이 생활을 하는. 실업자는 아니지만, 부랑 생활을 하는

0개의 댓글