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

@figliolia/react-metrics

v1.0.6

Published

React bindings for @figliolia/metrics

Downloads

9

Readme

React Metrics

React bindings for @figliolia/metrics!

This library is designed to make integrating Metric events with React rendering simple and effective. Often times, starting or stopping a Metric or interaction begins or ends with a Component reaching the browser and rendering to the screen. For this purpose, in this package you'll find utilities such as <MetricStart />, <MetricStop />, <MetricSubscription />, as well as their React hook equivalents!

Installation

npm i -S @figliolia/react-metrics
# or
yarn add @figliolia/react-metrics

Getting Started

StartMetric & StopMetric

import { Metric } from "@figliolia/metrics";
import { StartMetric, StopMetric } from "@figliolia/react-metrics"
 
const MyMetric = new Metric("My Metric");
 
export const MyComponent = () => {
  const [data, setData] = useState(null);
  useEffect(() => {
    fetch("/data").then(async data => {
      setData(await data.json());
    });
    return () => {
      MyMetric.reset();
    }
  }, []);
 
  if(!data) {
    return (
      <Fragment>
        <StartMetric metric={MyMetric}> // Calls your Metric's start() method on mount
        <Spinner />
      </Fragment>
    );
  }
 
  return (
    <Fragment>
      <InteractiveUI data={data} />
      <StopMetric metric={MyMetric} /> // Calls your Metric's stop() method on mount
    </Fragment>
  );
}

useStartMetric & useStopMetric

Swapping your <StartMetric /> for useStartMetric():

import { Metric } from "@figliolia/metrics";
import { useStartMetric } from "@figliolia/react-metrics";

const MyMetric = new Metric("My Metric");

export const MyComponent = () => {
  const [data, setData] = useState(null);
	// Start Metric on mount!
  useStartMetric(MyMetric);
  useEffect(() => {
    fetch("/data").then(async data => {
      setData(await data.json());
    });
    return () => {
      MyMetric.reset();
    }
  }, []);

  if(!data) {
    return <Spinner />;
  }

  return (
    <InteractiveUI data={data} />
    // Stop Metric when the UI is fully interactive!
    <StopMetric metric={MyMetric} />
  );
}

Swapping your <StopMetric /> for useStopMetric():

import { Metric } from "@figliolia/metrics";
import { useStopMetric } from "@figliolia/react-metrics";

const MyMetric = new Metric("My Metric");

export const MyComponent: FC<{ data: string[] }> = ({ data }) => {
  // Stops the provided metric on mount!
  useStopMetric(MyMetric);
  return (
    <ol>
      {data.map(item => {
        return <li key={item}>{item}</li>
      })}
    </ol>
  );
}

MetricSubscription & useMetricSubscription

Metrics provide pub-sub functionality useful for executing behaviors based upon the completion, success, failure, or duration of a given Metric. The <MetricSubscription> and useMetricSubscription() utilities allow developers to declare Metric subscriptions right from their React code - and have their subscriptions bound automatically to the Component's lifecycle:

import type { Metric } from "@figliolia/metrics";
import { MetricSubscription } from "@figliolia/metrics";
 
const DurationDisplay: FC<{ metric: Metric<any, any >}> = ({ metric }) => {
  const [duration, setDuration] = useState<number | null>(null);
 
  return (
    <Fragment>
      <MetricSubscription
        event="stop"
        metric={metric}
        callback={metric => {
          setDuration(metric.duration)
        }} />
      {
        duration === null ?
         <div>{metric.name} is currently {metric.status}</div>
        :
         <div>{metric.name} is complete! The duration was {duration} milliseconds</div>
      }
    </Fragment>
  );
}

Similarly, the same functionality can be achieved using the useMetricSubscription() hook:

import type { Metric } from "@figliolia/metrics";
import { useMetricSubscription } from "@figliolia/metrics";
 
const DurationDisplay: FC<{ metric: Metric<any, any>}> = ({ metric }) => {
  const [duration, setDuration] = useState<number | null>(null);
 
  useMetricSubscription({
    metric,
    event: "stop",
    callback: (metric) => {
      setDuration(metric.duration);
    }
  });
 
  if(duration === null) {
    return <div>{metric.name} is currently {metric.status}</div>
  }
 
  return <div>{metric.name} is complete! The duration was {duration} milliseconds</div>
}

Demo Application

To find some recipes in an example application, please reference our Demo App