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-defer-until-interaction

v1.0.3

Published

Delay loading and rendering until the user interacts with the page

Downloads

47

Readme

react-defer-until-interaction

Delay loading and rendering until the user interacts with the page

Features

  • Reduces page weight by helping you render only the most important content up front
  • With Next.js, it can give you fresh information after each route change, treating each page individually
  • Can help to improve your Lighthouse score by deferring less important and/or heavy content until its necessary

Installation & setup

yarn add react-defer-until-interaction

# or

npm install react-defer-until-interaction

Wrap your component (or the whole app) in the provider:

import { DeferUntilInteractionProvider } from 'react-defer-until-interaction'

function App() {
    return (
        <DeferUntilInteractionProvider>
            <MyContents />
        </DeferUntilInteractionProvider>
    )
}

Options

Next.js

In a Next.js app, add the provider to your _app.tsx, and pass the NextRouter:

// _app.tsx
import { useRouter } from 'next/router'
import { DeferUntilInteractionProvider } from 'react-defer-until-interaction'

function App() {
    const router = useRouter()

    return (
        <DeferUntilInteractionProvider router={router}>
            <MyContents />
        </DeferUntilInteractionProvider>
    )
}

This will enable the module to re-run on each route change, as if the page was freshly loaded in the browser.

Timeout

The hook will indicate that the user has interacted with the page, even if the user has not actually clicked/scrolled/etc, once 10 seconds have elapsed. This is meant as a safety fallback in case the events are somehow not heard, and for pages that are opened in the background (i.e. in a separate browser tab).

You may override the default timeout of 10 seconds by passing your own value (in milliseconds):

import { DeferUntilInteractionProvider } from 'react-defer-until-interaction'

function App() {
    return (

        <DeferUntilInteractionProvider timeout={5000} /* Assume that interaction has happened after 5 seconds */>
            <MyContents />
        </DeferUntilInteractionProvider>
    )
}

You can also set timeout={0} to disable the timer.

Usage

The useDeferUntilInteraction hook exposes two properties, a boolean and a function.

  • hasInteracted will be true after the user interacts with the page, or when 10 seconds have elapsed
  • afterInteraction() takes a callback and only invokes it after the user has interacted with the page, or when 10 seconds have elapsed
import { useDeferUntilInteraction } from 'react-defer-until-interaction'

function MyComponent() {
    const { hasInteracted, afterInteraction } = useDeferUntilInteraction()

    return (
        <p>You {hasInteracted ? 'have' : 'have not'} interacted with the page.</p>
        {afterInteraction(() => <BelowTheFold />)}
    )
}