@truv/react
v2.4.1
Published
Truv Bridge initialization package for react
Readme
Truv React Bridge 
React library for integrating with the Truv Bridge and Embedded Orders
Install
npm install @truv/reactUsage
Modal
import { useState } from 'react';
import { TruvBridge } from '@truv/react';
const ModalBridge = () => {
const [isOpened, setOpened] = useState(false);
return (
<>
<button onClick={() => setOpened(true)}>Open</button>
<TruvBridge
bridgeToken="<bridge token>" // create it on your server via the Truv API: https://docs.truv.com/api-reference/bridge-token/users_tokens
onClose={() => setOpened(false)}
onSuccess={(publicToken, metaData) => {
console.log(publicToken, metaData);
}}
isOpened={isOpened}
/>
</>
);
}
Inline
import { useState } from 'react';
import { TruvBridgeInline } from '@truv/react';
const InlineBridge = () => {
const [isOpened, setOpened] = useState(false);
return (
<>
<button onClick={() => setOpened(true)}>Open</button>
<TruvBridgeInline
// We can pass the properties of the div element here
style={{ width: '300px', height: '500px', backgroundColor: 'red' }}
// Required parameters for opening the bridge
isOpened={isOpened}
bridgeParams={{
bridgeToken: "<bridge token>", // create it on your server via the Truv API: https://docs.truv.com/api-reference/bridge-token/users_tokens
onClose: () => setOpened(false),
onSuccess: () => {
console.log('Success');
}
}}
/>
</>
);
}
Deferred loading and a specific version
@truv/react starts loading the latest version of the Bridge script as soon as it is imported. Import from @truv/react/lazy instead to decide when the script is loaded and to pin its version. The components are the same:
import { useState } from 'react';
import { TruvBridge, load } from '@truv/react/lazy';
// The latest version
load();
// or a specific version, loaded from https://cdn.truv.com/bridge/vX.X.X/bridge.js
load({ source: 'cdn', version: 'vX.X.X' });
// or a specific version checked with integrity (generate the hash yourself or see below)
load({ source: 'cdn', version: 'vX.X.X', integrity: 'sha384-<hash of the script>' });
// or the script you included on the page yourself, see "Bridge script included on the page" below
load({ source: 'window' });
const ModalBridge = () => {
const [isOpened, setOpened] = useState(false);
return (
<>
<button onClick={() => setOpened(true)}>Open</button>
<TruvBridge
bridgeToken="<bridge token>"
onClose={() => setOpened(false)}
onSuccess={(publicToken, metaData) => {
console.log(publicToken, metaData);
}}
isOpened={isOpened}
/>
</>
);
}How it works:
load()must be called: until then the components wait for it, and the widget never opens.load()can be called any number of times, only the first call is used.load()can also be called after the components are rendered, but the earlier the script starts loading, the sooner the widget opens. Call it as early as possible, e.g. on app start.
Warning: use either
@truv/reactor@truv/react/lazyin an app, not both:@truv/reactcallsload()on import, so the options passed toload()are ignored. Also don't use@truv/bridgein the same app: the script would be loaded twice.
The latest version and its Subresource Integrity hash are listed in latest.json. Details of a specific version, including its hash, are in https://cdn.truv.com/bridge/vX.X.X/manifest.json. Copy the version and the hash into your code rather than fetching these files at runtime, so the hash does not come from the same CDN as the script.
For a complete guide to pinning the Bridge version, see the Truv documentation.
Bridge script included on the page
If you include the Bridge script on the page yourself, pass source: 'window' to load() to use window.TruvBridge instead of loading the script again:
<!-- crossorigin is required for integrity -->
<script
src="https://cdn.truv.com/bridge/vX.X.X/bridge.js"
integrity="sha384-<hash of the script>"
crossorigin="anonymous"
></script>import { TruvBridge, load } from '@truv/react/lazy';
load({ source: 'window' });
const ModalBridge = () => (
<TruvBridge
// ...
/>
);- The script is never loaded from the CDN in this mode. If
window.TruvBridgeis not defined, the widget doesn't open and an error is thrown. - The Bridge script must run before the components open the widget: put its
<script>tag withoutasyncbefore the script of your app.
