Code Monkey home page Code Monkey logo

react-virtual-container's Introduction

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.

react-virtual-container's People

Contributors

ctrlplusb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

vishalmarakana

react-virtual-container's Issues

Clarify usage with optimistic

Hi @ctrlplusb, back with another usage question! I notice that in the docs you mention that if optimistic is used then the placeholder is rendered first before the children. Shouldn't that always be the case?

I noticed that if I don't provide a minimum height to Styled Virtual Container, then it renders all the children even though those children have a minimum height. Is that expected?

infinite loops with inAndOut enabled

Hi @ctrlplusb! Excited about this component - thanks for sharing!

I am using it on a feed where I fetch content as a user scrolls. I noticed that if you have inAndOut prop enabled, it leads to an infinite loop with the components re-rendering (even without scrolling up). So, it freezes or crashes the browser in just a few seconds :/

Any ideas on what might be going on?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.