UNPKG

2.96 kBMarkdownView Raw
1# `@shopify/react-import-remote`
2
3[![Build Status](https://travis-ci.org/Shopify/quilt.svg?branch=master)](https://travis-ci.org/Shopify/quilt)
4[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Freact-import-remote.svg)](https://badge.fury.io/js/%40shopify%2Freact-import-remote.svg) [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@shopify/react-import-remote.svg)](https://img.shields.io/bundlephobia/minzip/@shopify/react-import-remote.svg)
5
6Asynchronous script loading for React
7
8## Installation
9
10```bash
11$ yarn add @shopify/react-import-remote
12```
13
14## Usage
15
16The package provides a hook and component that are intended for loading external scripts. These utilities cache results by source, so only a single `script` tag is ever added for a particular source.
17
18### useImportRemote()
19
20```tsx
21import React from 'react';
22import {useImportRemote, Status} from '@shopify/react-import-remote';
23import {DeferTiming} from '@shopify/async';
24
25function MyComponent() {
26 const {result} = useImportRemote(
27 'https://some-external-service.com/global.js',
28 );
29
30 if (result.status === Status.Failed) {
31 // do something with error result
32 }
33
34 if (result.status === Status.Complete) {
35 // do something with successful result
36 }
37
38 return null;
39}
40```
41
42### <ImportRemote />
43
44```tsx
45import React from 'react';
46import ImportRemote from '@shopify/react-import-remote';
47import {DeferTiming} from '@shopify/async';
48
49interface RemoteGlobal {}
50interface WindowWithGlobal extends Window {
51 remoteGlobal: RemoteGlobal;
52}
53
54function MyComponent() {
55 return (
56 <ImportRemote
57 preconnect
58 source="https://some-external-service.com/global.js"
59 getImport={}
60 onImported={(result: RemoteGlobal | Error) => {
61 if (result instanceof Error) {
62 // do something with error result
63 }
64
65 // do something with successful result
66 }}
67 defer={DeferTiming.Mount}
68 />
69 );
70}
71```
72
73**source**
74
75Source of the script to load the global from
76
77**preconnect**
78
79Generates a preconnect link tag for the source’s domain using `<Preconnect />` component from [`@shopify/react-html`](../react-html)
80
81**getImport**
82
83Callback that takes in `window` with the added global and returns the global added to the `window` by the new script
84
85**onImported**
86
87Callback that gets called with the imported global or an `error` if one occurs
88
89**defer**
90
91A member of the `DeferTiming` enum (from `@shopify/async`) allowing the import request to wait until:
92
93- Component mount (`DeferTiming.Mount`; this is the default)
94- Browser idle (`DeferTiming.Idle`; if `window.requestIdleCallback` is not available, it will load on mount), or
95- Component is in the viewport (`DeferTiming.InViewport`; if `IntersectionObserver` is not available, it will load on mount)
96
97Note, changing any of these values while rendering will cancel the import.