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

@ivliu/react-offscreen

v1.0.7

Published

![NPM Version](https://img.shields.io/npm/v/%40ivliu%2Freact-offscreen) ![License](https://img.shields.io/badge/license-MIT-yellow)

Downloads

1,415

Readme

react-offscreen(activity)

NPM Version License

react-offscreen can hide components without uninstalling them

Features

  • based on Suspense
  • minzip only 1.2kb
  • good performance
  • react full context support

Installation

npm install @ivliu/react-offscreen
yarn add @ivliu/react-offscreen
pnpm add @ivliu/react-offscreen

Examples

Basic usage

import { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { Activity } from '@ivliu/react-offscreen';

const Counter = () => {
  const [count, setCount] = useState(0);

  return <p onClick={() => setCount(count + 1)}>{count}</p>;
};

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setVisible(!open)}>{open}</button>
      <Activity mode={open ? 'visible' : 'hidden'}>
        <Counter />
      </Activity>
    </div>
  );
};

ReactDOM.createRoot(document.getElementById('root')!).render(<App />);

Use with createPortal

import { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { createPortal } from 'react-dom';
import { Activity } from '@ivliu/react-offscreen';

const Counter = () => {
  const [count, setCount] = useState(0);

  return createPortal(
    <button type="button" onClick={() => setCount(count + 1)}>count is {count}</button>,
    document.body,
  );
};

const App = () => {
  const [open, setOpen] = useState(false);
  return (
    <div>
      <button onClick={() => setVisible(!open)}>{open}</button>
      <Activity mode={open ? 'visible' : 'hidden'}>
        <Counter />
      </Activity>
    </div>
  );
};

ReactDOM.createRoot(document.getElementById('root')!).render(<App />);

Use with React.lazy

Since Activity is implemented based on Suspense, please pay attention to placing the Suspense component under the Activity component when using it, otherwise it may cause the problem that the fallback cannot be displayed normally.

import { useState, lazy, Suspense } from 'react';
import ReactDOM from 'react-dom/client';
import { Activity } from '@ivliu/react-offscreen';

const LazyCount = lazy(() => import('./Count')); 

const Count = () => {
  const [count, setCount] = useState(0);

  return <p onClick={() => setCount(count + 1)}>{count}</p>;
};

const App = () => {
  const [open, setOpen] = useState(false);
  return (
    <div>
      <button onClick={() => setVisible(!open)}>{open}</button>
      <Activity mode={open ? 'visible' : 'hidden'}>
        <Suspense fallback="loading...">
          <LazyCount />
        </Suspense>
      </Activity>
    </div>
  );
};

ReactDOM.createRoot(document.getElementById('root')!).render(<App />);

Rename to Activity

In order to keep pace with the official react, we renamed Offscreen to Activity. At the same time, we will still export Offscreen

import { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { Activity, Offscreen } from '@ivliu/react-offscreen';

const Count = () => {
  const [count, setCount] = useState(0);

  return <p onClick={() => setCount(count + 1)}>{count}</p>;
};

const App = () => {
  const [open, setOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setVisible(!open)}>{open}</button>
      <Activity mode={open ? 'visible' : 'hidden'}>
        <Count />
      </Activity>
      <Offscreen mode={open ? 'visible' : 'hidden'}>
        <Count />
      </Offscreen>
    </div>
  );
};

ReactDOM.createRoot(document.getElementById('root')!).render(<App />);

typescript

import { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { Activity } from '@ivliu/react-offscreen';
import type { ActivityMode } from '@ivliu/react-offscreen';

const Count = () => {
  const [count, setCount] = useState(0);

  return <p onClick={() => setCount(count + 1)}>{count}</p>;
};

const App = () => {
  const [mode, setMode] = useState<ActivityMode>('visible');

  return (
    <div>
      <button onClick={() => setMode(mode === 'visible' ? 'hidden' : 'visible')}>{mode}</button>
      <Activity mode={mode}>
        <Count />
      </Activity>
    </div>
  );
};

ReactDOM.createRoot(document.getElementById('root')!).render(<App />);

unstable hooks

We provide hook implementation for component activation and deactivation status, but we do not plan to merge it into the main branch. If you need it, please refer to https://github.com/IVLIU/react-offscreen/tree/feat/unstable-hooks

import React from 'react';
import ReactDOM from 'react-dom/client';
import { Offscreen, useActivated } from 'react-offscreen';

const Count = () => {
  const [count, setCount] = React.useState(0);

  useActivated(() => {
    console.log('activated');
    return () => {
      console.log('deactivated')
    }
  });

  return <p onClick={() => setCount(count + 1)}>{count}</p>;
};

const App = () => {
  const [visible, setVisible] = React.useState(false);
  return (
    <div>
      <button onClick={() => setVisible(!visible)}>{visible}</button>
      <Offscreen mode={visible ? 'visible' : 'hidden'}>
        <Count />
      </Offscreen>
    </div>
  );
};

ReactDOM.createRoot(document.getElementById('root')!).render(<App />);

Notice

please use react16.8 and above versions