All Projects → ctrlplusb → react-virtual-container

ctrlplusb / react-virtual-container

Licence: MIT license
Optimise your React apps by only rendering components when in proximity to the viewport.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-virtual-container

home-space
Startpage and WebXR home
Stars: ✭ 43 (-17.31%)
Mutual labels:  virtual
react-combine-contexts
🚀Use multiple React Contexts without pain.
Stars: ✭ 23 (-55.77%)
Mutual labels:  render-props
robinjs-website
Alexa like assistant in 40 lines of code
Stars: ✭ 31 (-40.38%)
Mutual labels:  virtual
virtual touchscreen
Simple evdev linux device driver and GUI program to simulate multitouch touchscreen
Stars: ✭ 76 (+46.15%)
Mutual labels:  virtual
Indra Bot
Indra-A virtual assistant, she will give useful methods for preventing ourselves from the corona(Ayurvedic measures).
Stars: ✭ 12 (-76.92%)
Mutual labels:  virtual
repromised
🤝 Declarative promise resolver as a render props component
Stars: ✭ 20 (-61.54%)
Mutual labels:  render-props
stickyard
Make your React component sticky the easy way
Stars: ✭ 83 (+59.62%)
Mutual labels:  render-props
redux-render
Ergonomic Redux bindings for React using render functions
Stars: ✭ 54 (+3.85%)
Mutual labels:  render-props
react-callbag-listener
👂 A React render-prop component that listens to values emitted by callbags
Stars: ✭ 21 (-59.62%)
Mutual labels:  render-props
visage
Add virtual makeup to picture of a face.
Stars: ✭ 97 (+86.54%)
Mutual labels:  virtual
Virtual-Jaguar-Rx
Virtual Jaguar, an Atari Jaguar emulator, with integrated debugger
Stars: ✭ 35 (-32.69%)
Mutual labels:  virtual
paginated
⚛️ React render props component & custom hook for pagination.
Stars: ✭ 20 (-61.54%)
Mutual labels:  render-props
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-75%)
Mutual labels:  render-props
VAOS
Virtual Aviation Operations System
Stars: ✭ 44 (-15.38%)
Mutual labels:  virtual
MoPlugs
MotionBuilder Extensions Pack
Stars: ✭ 27 (-48.08%)
Mutual labels:  virtual
redebounce
↘️ Render Props component to debounce the given value
Stars: ✭ 14 (-73.08%)
Mutual labels:  render-props
janusweb
An in-browser implementation of JanusVR
Stars: ✭ 145 (+178.85%)
Mutual labels:  virtual
react-gearbox
⚙️📦 Gearbox - Renderless state provisioning and composition
Stars: ✭ 31 (-40.38%)
Mutual labels:  render-props
l1vm
L1VM - a tiny virtual machine with a 64 bit core
Stars: ✭ 112 (+115.38%)
Mutual labels:  virtual
kapp
A pure Rust window and input library for Windows, Mac, and Web. (Work in progress)
Stars: ✭ 58 (+11.54%)
Mutual labels:  windowing

react-virtual-container

Optimise your React apps by only rendering your components when they are in proximity to the viewport.

npm MIT License Travis Codecov

Table of Contents

Introduction

This library provides you with the ability to create a "virtual container", where it's children will only get rendered if the "virtual container" is within a given proximity of the viewport. This provides you with a nice mechanism by which to lazy load images or "heavy" components.

Installation

yarn add react-virtual-container

Or, if you prefer npm:

npm install react-virtual-container

Example

In the example below you will note two virtual containers. As the viewport moves down the page it triggers each virtual container causing the associated component to render and replace their placeholder.

Example - Out Again

In this example we expand on the behaviour of our virtual containers by setting the inAndOut prop. With this prop enabled as the viewport moves away from the virtual containers the components will again be replaced by their placeholders. This can be useful for cases where the components being virtualised consume a heavy amount of CPU/memory resources.

Usage

This library follows the "render prop" pattern, allowing you to specify a render or children prop. The "render prop" will be called and have its result rendered when the virtual container is within proximity of the viewport.

By default the virtual container needs to be within "50vh" proximity of the viewport. This is configurable - see configuration. You could set it to a pixel value or any other standard CSS unit value.

In addition to the "render prop" a useful prop is placeholder; it allows you to provide a component which will be rendered by the virtual container until it is within proximity of the viewport. This allows you to avoid layout that jumps when the virtual container activates. The placeholder prop is optional, however, we do recommend using it where you expect content jumping to occur.

import React from 'react'
import VirtualContainer from 'react-virtual-container'

const Placeholder = () => <div>🙈</div>

export default function MyVirtualisedComponent() {
  return (
    <VirtualContainer placeholder={Placeholder}>
      {() => <div>🐵</div>}
    </VirtualContainer>
  )
}

Configuration

The virtual container component accepts the following props:

  • children (PropTypes.func)

    The "render prop" responsible for returning the elements you wish to render should the virtual container come within proximity of the viewport. You can alternatively use the render prop.

  • className (PropTypes.string, optional)

    A class to apply to the virtual container element.

    Note: when providing a style for your virtual container element you MUST ensure that you have a "position" value set. This is because we use a set of absolutely positioned elements by which to track the proximity.

  • el (PropTypes.string, default: "div")

    The type of element that the virtual container should render as.

    Your render prop results will render as children to this element.

  • inAndOut (PropTypes.bool, default: false)

    If you enable this prop, then your component will be removed (or replaced with the placeholder if one was defined) when the viewport moves out of proximity to the virtual container.

    This can be especially useful for component that heavily use CPU/memory resources.

  • offsetBottom (PropTypes.oneOfType([PropTypes.string, PropTypes.number]), default: "50vh")

    The proximity value that will trigger rendering of your "render prop" when the virtual container is within the specified distance relative to the bottom of the view port.

  • offsetTop (PropTypes.oneOfType([PropTypes.string, PropTypes.number]), default: "50vh")

    The proximity value that will trigger rendering of your "render prop" when the virtual container is within the specified distance relative to the top of the view port.

  • onChange (PropTypes.func)

    If provided, this callback function will be called any time the virtualisation value changes. It recieves a single boolean parameter, being true when virtualisation is active, and false when it is not.

  • optimistic (PropTypes.bool, default: false)

    If you set this then the placeholder will be rendered before the "render prop" whilst we determine if the virtual container is within proximity of the viewport. You should almost never use this.

  • placeholder (PropTypes.func)

    A placeholder component/function that will be rendered when the virtual container is not within proximity of the viewport. Useful to avoid your page jumping with new content being inserted (especially when scrolling from the bottom of the page up).

  • render (PropTypes.func)

    The "render prop" responsible for returning the elements you wish to render should the virtual container come within proximity of the viewport. You can alternatively use the children prop.

  • scrollableAncestor (PropTypes.any)

    When tracking proximity we use scroll positioning. Depending on the structure of your DOM you may want to explicitly define the element by which your scrolling is bound. For example this could be a value of "window" or a direct reference to a DOM node.

  • style (PropTypes.object)

    A style to provide to the virtual container element.

    Note: when providing a style for your virtual container element you MUST ensure that you have a "position" value set. This is because we use a set of absolutely positioned elements by which to track the proximity.

Tips and Tricks

Below are some useful tips for using this library effectively within your app.

Usage with Styled Components or Emotion

The VirtualContainer component produces an actual DOM element - a div by default, although this is configurable via the el prop.

What if you want to style the element via Styled Components or Emotion, two very popular CSS-in-JS libraries.

You can completely do so by wrapping the VirtualContainer with the style function. Styled Components / Emotion will then pass down a className to the VirtualContainer, which it supports as a prop. The className will be applied to the element.

We will update the example from earlier to do so.

import styled from 'react-emotion'
import VirtualContainer from 'react-virtual-container'

const StyledVirtualContainer = styled(VirtualContainer)`
  position: relative;
  height: 100px;
  background-color: pink;
`

export default function MyVirtualisedComponent() {
  return (
    //                        👇 you can still pass down configuration! woot!
    <StyledVirtualContainer inAndOut>
      {() => <div>🐵</div>}
    </StyledVirtualContainer>
  )
}

Awesome! This is a pretty powerful technique, and can aid in avoiding having to use a placeholder.

Note: when providing your own styled, please make sure you set a "position" style on your component. This is because internally we have some relatively positioned elements which are rendered as children in order to tracking of viewport proximity.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].