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 |
|
6 | Asynchronous script loading for React
|
7 |
|
8 | ## Installation
|
9 |
|
10 | ```bash
|
11 | $ yarn add @shopify/react-import-remote
|
12 | ```
|
13 |
|
14 | ## Usage
|
15 |
|
16 | The 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
|
21 | import React from 'react';
|
22 | import {useImportRemote, Status} from '@shopify/react-import-remote';
|
23 | import {DeferTiming} from '@shopify/async';
|
24 |
|
25 | function 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
|
45 | import React from 'react';
|
46 | import ImportRemote from '@shopify/react-import-remote';
|
47 | import {DeferTiming} from '@shopify/async';
|
48 |
|
49 | interface RemoteGlobal {}
|
50 | interface WindowWithGlobal extends Window {
|
51 | remoteGlobal: RemoteGlobal;
|
52 | }
|
53 |
|
54 | function 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 |
|
75 | Source of the script to load the global from
|
76 |
|
77 | **preconnect**
|
78 |
|
79 | Generates a preconnect link tag for the source’s domain using `<Preconnect />` component from [`@shopify/react-html`](../react-html)
|
80 |
|
81 | **getImport**
|
82 |
|
83 | Callback that takes in `window` with the added global and returns the global added to the `window` by the new script
|
84 |
|
85 | **onImported**
|
86 |
|
87 | Callback that gets called with the imported global or an `error` if one occurs
|
88 |
|
89 | **defer**
|
90 |
|
91 | A 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 |
|
97 | Note, changing any of these values while rendering will cancel the import.
|