@shopify/argo-checkout-react
React bindings for @shopify/argo-checkout
Last updated 19 hours ago by shopify-dep .
MIT · Original npm · Tarball · package.json
$ cnpm install @shopify/argo-checkout-react 
SYNC missed versions from official npm registry.

@shopify/argo-checkout-react

This library provides utilities for writing Argo extensions for Checkout using React.

Installation

$ yarn add @shopify/argo-checkout-react

Usage

Argo’s UI rendering capabilities are built on top of a library called remote-ui. One of the powerful aspects of remote-ui is that it can be the target for JavaScript libraries that support custom (non-DOM) rendering. React is one such library, and remote-ui provides a custom renderer for React that allows you to use all the power of React, while outputting UI mutations that Argo can understand.

@shopify/argo-checkout-react provides a few additional utilities if you are writing an Argo extension in React (it also re-exports all the relevant values from @shopify/argo-checkout). Before you start down this path, though, make sure you are working on an extension that will be complex enough to warrant the additional bundle size of including React. The core API of remote-ui is built to be usable directly, and remote-ui provides other bindings, like this one to htm, that provide some of the ergonomics of React without so much overhead. For additional details, please read the performance guide.

To use the React bindings in Argo for Checkout, start by importing React as you normally would. All of the core features of React are available, including hooks, context, and more.

You’ll then import render from @shopify/argo-checkout-react. This function is a thing wrapper around shopify.extend and @remote-ui/react’s render(). You’ll pass this function the name of an extension that can render UI, and a function that should return the JSX to render when that extension point is run. This function receives the input argument for the extension point.

import React from 'react';
import {render} from '@shopify/argo-checkout';

// `extensionPoint` is part of the [standard API](../argo-checkout/src/extension-points/api/standard)
render('Checkout::PostPurchase::Render', ({extensionPoint}) => (
  <App extensionPoint={extensionPoint} />
));

interface Props {
  extensionPoint: string,
}

function App({extensionPoint}: Props) {
  return <>Extension point: {extensionPoint}</>;
}

If you’ve ever used React on the web, you’re probably used to returning DOM nodes as part of your React components. Because Argo extensions execute in a web worker and have no access to the DOM, returning DOM components is an error in Argo extensions. Instead, you can return the components you import from @shopify/argo-checkout-react, which are the equivalent of the DOM in Argo — they are the “leaf” elements, the lowest-level UI primitives that exist.

import React from 'react';
import {render, Button} from '@shopify/argo-checkout';

render('Checkout::PostPurchase::Render', (props) => (
  <App {...props} />
));

interface Props {
  extensionPoint: string,
}

function App({extensionPoint}: Props) {
  return (
    <Button
      onPress={() => {
        console.log(`Extension point: ${extensionPoint}`);
      }}
    >
      Log extension point to console
    </Button>
  );
}

Other React-specific APIs

useExtensionInput()

useExtensionInput is a custom React hook that gives you access to the full input argument provided to your extension point (this is the value that, if you were registering an extension point directly with shopify.extend, would be passed as the second argument to your callback). This allows you to access and call the main APIs between your extension and Shopify anywhere in your React component.

If you are using TypeScript, you can supply the name of the extension point as a type parameter to this function. Doing so will refine the return type to be exactly the input type for that extension point, so make sure you pass the name of the extension you are actually rendering.

import React from 'react';
import {render, useExtensionInput, Button} from '@shopify/argo-checkout';

render('Checkout::PostPurchase::Render', () => <App />);

function App() {
  const {extensionPoint} = useExtensionInput<
    'Checkout::PostPurchase::Render'
  >();

  return (
    <Button
      onPress={() => {
        console.log(`Extension point: ${extensionPoint}`);
      }}
    >
      Log extension point to console
    </Button>
  );
}

This hook can only be called if you registered your extension with render, as that callback wraps the application in the necessary React context to make the input available anywhere in the tree.

Current Tags

  • 0.5.3                                ...           latest (19 hours ago)
  • 0.3.9-beta.0                                ...           next (a month ago)

24 Versions

  • 0.5.3                                ...           19 hours ago
  • 0.5.2                                ...           5 days ago
  • 0.5.1                                ...           8 days ago
  • 0.5.0                                ...           13 days ago
  • 0.4.2                                ...           19 days ago
  • 0.4.1                                ...           a month ago
  • 0.4.0                                ...           a month ago
  • 0.3.10                                ...           a month ago
  • 0.3.9                                ...           a month ago
  • 0.3.9-beta.0                                ...           a month ago
  • 0.3.8                                ...           a month ago
  • 0.3.7                                ...           2 months ago
  • 0.3.6                                ...           2 months ago
  • 0.3.5                                ...           2 months ago
  • 0.3.4                                ...           3 months ago
  • 0.3.3                                ...           3 months ago
  • 0.3.2                                ...           3 months ago
  • 0.3.1                                ...           3 months ago
  • 0.3.0                                ...           3 months ago
  • 0.2.0                                ...           3 months ago
  • 0.1.3                                ...           3 months ago
  • 0.1.2                                ...           3 months ago
  • 0.1.1                                ...           3 months ago
  • 0.1.0                                ...           3 months ago
Downloads
Today 2
This Week 9
This Month 82
Last Day 7
Last Week 20
Last Month 67
Dependencies (3)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |