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

@salable/react-utils

v4.0.0

Published

A small set of hooks and utilities for building React apps w/ Salable.app

Downloads

3

Readme

Salable React Utils

A set of hooks, contexts and utilities that will come in handy when building React applications with Salable.

Each function/hook/util in this library has detailed docblocks - hover over any of them in your code editor to learn more.

SalableContextProvider

In order to make use of any of the hooks in this package, you will need to render a SalableContextProvider in your application. Once this is rendered, all children of that component will be able to utilise the provided hooks. For simplicity's sake, we recommend rendering this at the top level of your application.

Both apiKey and productUuid are required when rendering the provider.

Example

import { SalableContextProvider } from '@salable/react-utils';

function MyApp({ children }) {
  return (
    <SalableContextProvider
      value={{
        apiKey: 'YOUR_API_KEY',
        productUuid: 'YOUR_PRODUCT_UUID',
        granteeId: 'OPTIONAL_GRANTEE_ID',
      }}
    >
      {children}
    </SalableContextProvider>
  );
}

useSalableContext

Fetches the SalableContext data. This is mostly used by the other hooks in this package, but is exposed in-case you want access to the underlying values.

Example

import { useSalableContext } from '@salable/react-utils';

function MyComponent() {
  const contextData = useSalableContext();
  return <></>;
}

You can also use useSalableContext to update the underlying granteeId value relied on by other hooks in this package.

Example

import { useSalableContext } from '@salable/react-utils';

function MyComponent() {
  const contextData = useSalableContext();

  function clickHandler() {
    contextData.setGranteeId('the-new-grantee-id');
  }

  return <button onClick={clickHandler}>Change user</button>;
}

useUser

Returns useful data scoped both to the provided user (determined by the granteeId from the SalableContext) AND the provided product.

Also returns a hasCapability function which allows for simple checking of the provided user's capabilities.

Example

import { useUser } from '@salable/react-utils'

function ActionButtons() {
  const user = useUser();

  if (user.state === 'loading') return <LoadingSpinner />
  if (user.state === 'error') {
    // Handle errors here...
  }

  /**
   * If you only want to check a single capability, pass in a string rather than
   * an array.
   *
   * hasCapability('test') => Boolean
   */
  const { edit, delete } = user.hasCapability(['edit', 'delete'])

  return (
    <div>
      <button>Cancel</button>
      {edit ? <button>Edit</button> : null}
      {delete ? <button>Delete</button> : null}
    </div>
  )
}

useProduct

Returns useful data about the current product. Can be used for many things including the creation of custom pricing tables.

By default, plans and features that are marked as DEPRECATED will be excluded from the response. If you would like these returned, you can pass in an optional options object with { withDeprecated: true }.

Example

import { useProduct } from '@salable/react-utils';

function MyComponent() {
  const { name } = useProduct();
  return <h1>This product is called {name}</h1>;
}