npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@parallelmarkets/react

v2.1.2

Published

ParallelMarkets.com React SDK

Downloads

5,782

Readme

Parallel Markets JavaScript React Components

CI Status npm version

This library provides a loading wrapper for the Parallel Markets JavaScript SDK with React components.

For a quick start, check out the example React app.

Installation

Use npm to install the Parallel JS SDK module and this React library:

$> npm install --save @parallelmarkets/react @parallelmarkets/vanilla

ParallelProvider

The ParallelProvider allows you to use our hooks and access the Parallel object in any nested component. Render a ParallelProvider at the root of your React app so that it is available everywhere you need it.

To use the ParallelProvider, call loadParallel from @parallelmarkets/vanilla with your configuration options. The loadParallel function asynchronously loads the parallel.js script and initializes a Parallel object. Pass the returned Promise to ParallelProvider.

import { loadParallel } from '@parallelmarkets/vanilla'
import { ParallelProvider } from '@parallelmarkets/react'

// Start loading the parallel library with the given configuration information. Make sure
// you call this outside of a component's render to avoid recreating a `Parallel` object
// on every render. Also - you should not "await" the resulting promise, just pass directly
// to the ParallelProvider in the "parallel" property
const parallel = loadParallel({ client_id: '123', environment: 'demo', flow_type: 'overlay' })

const App = () => (
  <ParallelProvider parallel={parallel}>
    <YourRootComponent />
  </ParallelProvider>
)

const app = document.getElementById('main')
const root = createRoot(app)
root.render(<App />)

[!NOTE] To best leverage Parallel's fraud detection, include the call to loadParallel across your app/site. This allows Parallel to detect suspicious behavior that may be indicative of fraud as users interact with your website.

With ParallelProvider in place, you can use the useParallel hook in any component to get access to a number of useful properties and functions:

  • parallel: This provides access to the full JS SDK
  • loginStatus: The current login status of the user. This is the result of a call to getLoginStatus(), which may be null initially until a call to the API finishes.
  • getProfile(): A function that returns a Promise that will be resolved with profile information (from a call to the Profile API)
  • login(): A shortcut to parallel.login()
  • logout(): A shortcut to parallel.logout()
  • error: Any error that resulted from getting the current login status on load

For instance:

Initiating a Parallel Flow

This is a more complete example, showing use of the useParallel hook in a child component. Additionally, this example shows use of the PassportButton component, that, when clicked, initiates a Parallel flow. You can simply call parallel.login() as an alternative to showing the PassportButton component.

import { loadParallel } from '@parallelmarkets/vanilla'
import { ParallelProvider, useParallel, PassportButton } from '@parallelmarkets/react'

const ParallelProfile = () => {
  // the parallel variable provides access to the full SDK available at
  // https://developer.parallelmarkets.com/docs/javascript/sdk
  const { parallel, loginStatus } = useParallel()

  // we may render before the loginStatus is available
  if (!loginStatus) return null

  return (
    <>
      <h1>Status: {loginStatus.status}</h2>
      {/* Only show the login button if the user hasn't logged in yet */}
      {loginStatus.status !== 'connected' ? (
        <PassportButton />
      ) : (
        <button onClick={parallel.logout}>Log Out</button>
      )}
    </>
  )
}

// start loading the parallel library with the given configuration information
// the resulting promise will be passed to the ParallelProvider
const parallel = loadParallel({ client_id: '123', environment: 'demo', flow_type: 'overlay' })

const App = () => (
  <ParallelProvider parallel={parallel}>
    <ParallelProfile />
  </ParallelProvider>
)

const root = createRoot(document.getElementById('main'))
root.render(<App />)

Getting the Parallel ID

The result of any successful authentication event will include an authResponse field that indicates the status of the handoff. Once the status is connected, you can call the getProfile() function to get the Parallel ID for the user or business that completed the flow (along with other profile information). The ID can then be saved to your backend so your servers can make ongoing calls to get/update information for the user/business.

Here's an example of a few lines you can add to the example above if you want to send the profile information (including Parallel ID) to your backend.

const ParallelIDSaver = () => {
  const { loginStatus, getProfile } = useParallel()

  useEffect(() => {
    if (loginStatus?.status !== 'connected') return

    // Call your backend to save the resulting ID to your backend so your server
    // can make ongoing calls to get/update information for this user/business.
    // In this example, your server would just save the resulting Parallel ID in
    // the profile information POSTed alongside the other investor profile information
    // you have stored on your backend.
    getProfile().then((profile) => {
      // assuming that getInvestorId() returns your unique ID for the user/business
      const body = JSON.stringify({ profile, internalId: getInvestorId() })
      fetch('/save-parallel-id', { method: 'POST', body })
    })
  }, [])

  // if the user hasn't connected yet or the library isn't yet loaded, we can't
  // show anything yet
  if (loginStatus?.status !== 'connected') return null

  return <p>Thanks for providing your information!</p>
}

Embed Flow Type

While the "overlay" and "redirect" flow_type options will work fine with React, the "embed" option will not. React recreates elements on render/re-render, causing any children iframe elements to be recreated, which results in reloading the URL in the src attribute. This causes a reload of the Parallel experience within the iframe, which is not an ideal experience for users.

This issue on React’s Github has more info about the effect. Any movement of the iframe element in the DOM (including recreating, as React does) will produce a re-fetching of the src URL of the iframe, resulting in a re-render of the Parallel experience.

We strongly recommend using the "overlay" flow_type if you're using React.