Giter Club home page Giter Club logo

react-page-visibility's Introduction

React Page Visibility

Declarative, nested, stateful, isomorphic page visibility for React

Build Status

Motivation

Are you polling your Backend on an interval basis? Are you running animations? What do you do if your tab is no longer visible?

See more classic use-cases in MDN Page Visibility API.

Well now you can react (Pun intended) to your app being in the background and invisible by conserving bandwidth and GPU calculations with ease. Introduction React Page Visibility:

  • A React higher order component that wraps the page visibility API
  • Cross-browser support (Yes, even IE and Safari)
  • Safe fallback if browser does not support it
  • Can be used multiple times anywhere in your application without side effects
  • Lets you decide how to handle the page being invisible and turning visible again

Why use a React component and not a helper function?

Because React is cool. 'Nuff said.

But really, why not use a helper function? Because you will then need to addEventListener and removeEventListener in your component lifecycle and that gets tedious.

Also, every time you use it you will need to check if your user's browser supports it and that gets tedious too.

Instead with react-page-visibility everything is taken care of for you.

Installation

$ npm install --save react-page-visibility

Usage

A rotating carousal component that will be passed down a prop of whether to rotate the images or not based on whether page is visible.

Using onChange callback

import React from 'react';
import PageVisibility from 'react-page-visibility';

class AppContainer extends React.Component {
    state = {
        rotate: true
    };

    handleVisibilityChange = isVisible => {
        this.setState({ rotate: !isVisible });
    }

    render() {
        return (
            <PageVisibility onChange={this.handleVisibilityChange}>
                <RotatingCarousel rotate={this.state.rotate} />
            </PageVisibility>
        );
    }
}

Using children as function callback

import React from 'react';
import PageVisibility from 'react-page-visibility';

const AppContainer = () => {
    return (
        <PageVisibility>
            { isVisible => <RotatingCarousel rotate={isVisible} /> }
        </PageVisibility>
    );
}

API

react-page-visibility is an higher order component, you can pass to it an onChange function:

onChange(handler)

Where handler is the callback to run when the visibilityState of the document changes:

Function handler(<Boolean> isVisible, <String> visibilityState)

  • isVisible is a Boolean indicating whether document is considered visible to the user or not.
  • visibilityState is a String and can be one of visible, hidden, prerender, unloaded (if your browser supports those)

Notice: previous versions had different arguments in the handler

Or you can use function as children pattern, where children is the callback to run when the visibilityState of the document changes.

Function children(<Boolean> isVisible, <String> visibilityState)

See MDN Page Visibility API Properties overview

License

MIT

react-page-visibility's People

Contributors

pgilad avatar clemencov avatar

Watchers

James Cloos avatar Jeremy Kay avatar

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.