question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi @roopemerikukka & @nirpeled, I noticed in another issue that you both mentioned using react-visibility-sensor in a production app. That’s great to hear! 😃

I’m trying to gather a set of officially supported demos that adequately show the different ways this component is being used in production. This will also help to ensure that as we modernise the codebase and refine the API, the features that are depended on take priority.

For example, if we look at this demo of scrolling through a list and activating items when they become visible, what are the differences between this and how you are using it? What would a demo that covered your use-case look like?

Thanks for your help!

PS. anyone else who is using this component in production, please join in the thread too! 😃

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nirpeledcommented, Sep 19, 2019

@alex-cory I used the ref.

Here’s the code I used about a year ago - should still work 😎

// inspired by
// https://github.com/yamill/react-native-inviewport
// which is inspired by
// https://github.com/joshwnj/react-visibility-sensor

import React from 'react';
import { View } from 'react-native';
import PropTypes from 'prop-types';
import globalStyles from 'app/constants/styles';

class VisibilitySensor extends React.Component {

    static propTypes = {
        active: PropTypes.bool,
        delay: PropTypes.number,
    };

    static defaultProps = {
        active: true,
        delay: 100,
    };

    state = {
        rectTop: 0,
        rectBottom: 0,
    };

    componentDidMount() {

        const { active } = this.props;

        if (active) {
            this.startWatching();
        }
    }

    componentWillUnmount() {

        this.stopWatching();

    }

    componentWillReceiveProps(nextProps) {

        const { active } = nextProps;

        if (active) {
            this.lastValue = null;
            this.startWatching();
        } else {
            this.stopWatching();
        }

    }

    startWatching = () => {

        const { delay } = this.props;

        if (this.interval) {
            return;
        }

        this.interval = setInterval(this.check, delay);

    };

    stopWatching = () => {

        this.interval = clearInterval(this.interval);

    };

    check = () => {

        const { onChange } = this.props;
        const ref = this.refs.view;
        const width = globalStyles.dimensions.getWidth();
        const height = globalStyles.dimensions.getHeight();

        ref.measure((ox, oy, width, height, pageX, pageY) => {

            this.setState({
                rectTop: pageY,
                rectBottom: pageY + height,
                rectWidth: pageX + width,
            })

        });

        const isVisible = (
            (this.state.rectBottom !== 0) &&
            (this.state.rectTop >= 0) &&
            (this.state.rectBottom <= height) &&
            (this.state.rectWidth > 0) &&
            (this.state.rectWidth <= width)
        );

        // notify the parent when the value changes

        if (this.lastValue !== isVisible) {
            this.lastValue = isVisible;
            onChange(isVisible);
        }

    };

    render() {

        const { active, delay, children } = this.props;

        return (
            <View ref="view" active={active} delay={delay}>
                {children}
            </View>
        );

    }

}

export default VisibilitySensor;
1reaction
joshwnjcommented, Sep 20, 2018

Good idea @agentmilindu . Let’s follow this up in #132 and then circle back here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Demos: Homepage
Demos is a dynamic “think-and-do” tank that powers the movement for a just, inclusive, multiracial democracy.
Read more >
Demos' Restaurants- Family Value, Family Recipes, Family ...
As a Greek immigrant family, the Demos family knew the importance of offering exceptional food with authentic ingredients at quality prices. Read our...
Read more >
Graphing Calculator - Desmos
Explore math with our beautiful, free online graphing calculator. Graph functions, plot points, visualize algebraic equations, add sliders, animate graphs, ...
Read more >
demos - Wiktionary
(originally Ancient Greece) An ancient subdivision of Attica; (now also) a Greek municipality, an administrative area covering a city or several villages ...
Read more >
Demos Definition & Meaning - Merriam-Webster
The meaning of DEMOS is populace.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found