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

@mpth/react-in-view

v1.0.3

Published

React component to wrap non SSR components and only when it comes in our view.

Downloads

7

Readme

@mpth/react-in-view

React component to wrap a non SSR component and only show it when it comes into our viewport, speeding up the eventloop while rendering pages SSR and only truly load/show it when it comes into the users view. Works with React >= 16.5.0

When working with Server Side Rendering(SSR) enabled apps, you have to deal with client only components. This wrapper makes it simple to work with those components when you wanted to load data and or when the component comes into the view. Make sure you have 'react-intersection-observer' installed in your application.

npmjs.com/package/@mpth/react-in-view

Installation

yarn add @mpth/react-in-view

Online demonstration

Heroku container spins down when no activity, please be patient!. See graphql-schiphol.herokuapp.com for a live demonstration. I will add some storybook/codepen examples overhere too but this lazy example shows how it can be used.

On line 13 we added the intersection-observer polyfill for IE11. The code for the page itself comes from lazy.js. Now if you watch this page when its spinned up you can see the last two calls are being only made when the user scrolls to them.

Usage

Foo is our client only component:

import React from 'react';
import InView from '@mpth/react-in-view';
import Foo from '../modules/foo';
import Bar from '../modules/bar';

const Page = () => (
    <>
        <h1>Page</h1>
        <Bar />
        <InView>
            <Foo />
        </InView>
    </>
);

Then, <Foo /> component is only rendered on the client just after it's mounted and in our view. Its possible to add css to the main component to have an aspect ratio to prevent reflows for example. Also can you tweak from how many pixels it starts painting it.

options:

  1. fallback
  2. rootMargin
  3. className

ad 1. Render a Component on SSR with a personal fallback

Usually, we need to add some loading text or similar until <Foo /> component starts to render. This fallback is rendered server-side. Here's how to do it.

const Loading = () => <div>Loading...</div>;
const Page = () => (
    <>
        ....
        <InView fallback={<Loading />}>
            <Foo />
        </InView2>
    </>
);

Now <Loading /> component will be rendered until <Foo /> component comes in the viewport.

ad 2.

Set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. For example '20px 0px 280px 0px'

ad 3.

Add your own css to the main container for example to set a min-height, or aspect ratio or preloading.

Codecov Coverage