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

@dopt/react-users

v0.2.9

Published

React hooks for identifying users and groups to Dopt's Users API

Downloads

222

Readme

React hooks for Dopt's Users API

Overview

The Dopt Users API hooks are React-specific utility for identifying users and groups to Dopt.

If you aren't using React and / or want a lower-level interface for identifying users and groups, check out @dopt/users-javascript-client.

This package published to npm as @dopt/react-users.

Installation

Via npm:

npm install @dopt/react-users

Via Yarn:

yarn add @dopt/react-users

Via pnpm:

pnpm add @dopt/react-users

Configuration

To configure the Dopt Users provider you will need:

  1. A users API key (generated in Dopt)

Usage

Initialization

You can initialize Dopt in your app by integrating the <DoptProvider /> as follows:

import { DoptUsersProvider } from '@dopt/react-users';
import Application from './application';

const rootElement = document.getElementById('root');
ReactDOM.render(
  <DoptUsersProvider apiKey={usersApiKey}>
    <Application />
  </DoptProvider>,
  rootElement
);

Example usage

Identifying a user

Helpful when initializing the DoptProvider from @dopt/react.

import { useMemo } from 'react';
import { DoptProvider } from '@dopt/react';
import { useIdentifyUser } from '@dopt/react-users';

export function Application() {
  /**
   * You will probably retrieve your user in a different manner.
   * This is just a randomly generated user singleton for
   * example purposes.
   */
  const user = useMemo(() => ({
    identifier: Math.random().toString(),
    properties: {
      role: 'test-user',
      inTrial: true,
    },
  }), []);

  /**
   * Before the API request for identifying the user
   * has finished, `useIdentifyUser` will return undefined.
   * With an undefined `userId`, the DoptProvider will not
   * fetch flow and block state.
   *
   * After the call has finished, `useIdentifyUser` will return
   * the user's identifier and will trigger the DoptProvider
   * to fetch and update flow and block state.
   */
  const userId = useIdentifyUser(user);

  return (
    <DoptProvider
      userId={userId}
      apiKey={blocksAPIKey}
      flows={{
        'new-user-onboarding': 2,
        'plan-upsell': 4,
      }}
    >
      <Application />
    </DoptProvider>,
  );
}

Identifying a group

Helpful when initializing the DoptProvider from @dopt/react with a groupId.

import { useIdentifyGroup } from '@dopt/react-users';

export function Application() {
  /**
   * You will probably retrieve your user and group
   * in a different manner. These are just randomly generated
   * user and group singletons for example purposes.
   */
  const user = useMemo(() => ({
    identifier: Math.random().toString(),
    properties: {
      role: 'test-user',
      inTrial: true,
    },
  }), []);

  const group = useMemo(() => ({
    identifier: Math.random().toString(),
    properties: {
      company: 'Dopt',
      paid: false,
    },
  }), []);

  const userId = useIdentifyUser(user);
  const groupId = useIdentifyGroup(group);

  return (
    <DoptProvider
      userId={userId}
      groupId={groupId}
      apiKey={blocksAPIKey}
      flows={{
        'new-user-onboarding': 2,
        'plan-upsell': 4,
      }}
    >
      <Application />
    </DoptProvider>,
  );
}

Feedback

Looking to provide feedback or report a bug? Open an issue or contact us at [email protected].

Contributing

All contributions are welcome! Feel free to open a pull request.