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

react-expiry

v1.0.2

Published

Hide React components based on expiry date/time

Downloads

264

Readme

react-expiry

Hide React components based on expiry date/time.

Installation

pnpm add react-expiry

Usage

// _app.tsx
import { ExpiryProvider } from "react-expiry";
import { Hello } from "./hello";

const App = () => {
  return (
    <ExpiryProvider>
      <Hello />
    </ExpiryProvider>
  );
};

// hello.tsx
import { useExpiry } from "react-expiry";

const Hello = () => {
  const showWelcomeMsg = useExpiry({
    id: "welcome-msg",
    ttl: 24 * 60 * 60, // 1 day in seconds
  });

  const showLaunchDate = useExpiry({
    id: "launch-date",
    expires: new Date("2024-10-03"),
  });

  const showNewFeature = useExpiry({
    id: "new-feature",
    expires: new Date("2025-01-01"),
    ttl: 5 * 24 * 60 * 60, // 5 days in seconds
  });

  return (
    <div>
      <h1>Hello World</h1>
      {showWelcomeMsg && (
        <p>After initial render, I will disappear after 1 day.</p>
      )}
      {showLaunchDate && (
        <p>I will always disappear by 3rd of October, 2024.</p>
      )}
      {showNewFeature && (
        <p>
          After initial render, I will disappear after 5 days or by 1st of
          January, 2025, whichever comes first.
        </p>
      )}
    </div>
  );
};

API

ExpiryProvider

defaultTTL (default: 0)

This is the default time-to-live (in seconds) for components. If a hook has a ttl option set, it will override this value.

keyPrefix (default: "expiry:")

This is the prefix for the keys in the storage. It is used to avoid conflicts between different components.

browserOnly (default: true)

Controls the rendering behavior when rendering on the server. If true, the component will only be rendered on the browser, otherwise it will be rendered on both the browser and the server.

storage (default: localStorage)

This is the storage used to persist the expiry. It defaults to localStorage.

useExpiry

id (required)

This is the identifier for the expiry and is used in the persistence key. It should be unique within your application.

ttl (optional)

This is the time-to-live (in seconds) for the expiry. When the ttl is reached, the expiry will return false and the component will be hidden.

NOTE: The use of ttl is per user session. If the user clears their storage, the expiry will be reset and the ttl will start again.

expires (optional)

This is the expiration date for the expiry. When the expires date is reached, the expiry will return false and the component will be hidden.

NOTE: The use of expires is global. It will hide the component for all users.

License

MIT

ESLint

You can install the companion ESLint plugin to add react-expiry rules to your linting.

pnpm add eslint-plugin-react-expiry

Then add these rules to your ESLint config.

{
  "plugins": ["react-expiry"],
  "rules": {
    "react-expiry/expires-in-past": "warn",
    "react-expiry/duplicate-id": "warn"
  }
}