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.

Any chance of getting a once prop, that would only trigger the event once ?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:17
  • Comments:9

github_iconTop GitHub Comments

3reactions
robbertvancaemcommented, Jul 7, 2020

Until #143 is merged, today I came up with this simple solution.

// components/sensor.js
import React, { useState } from "react";
import VisibilitySensor from "react-visibility-sensor";
import PropTypes from "prop-types";

const Sensor = ({ children, once }) => {
  const [visible, setVisible] = useState(false);

  return (
    <VisibilitySensor
      active={once ? !visible : true}
      onChange={(isVisible) => {
        if (visible && once) {
          return;
        }

        setVisible(isVisible);
      }}
    >
      {children({ isVisible: visible })}
    </VisibilitySensor>
  );
};

Sensor.propTypes = {
  children: PropTypes.func.isRequired,
  once: PropTypes.bool,
};

Sensor.defaultProps = {
  once: false,
};

export default Sensor;

You can then use it like so:

// components/something.js
import Sensor from './sensor';

<Sensor key={title} once>
    {({ isVisible }) => (<div className={`hello ${isVisible && "is-visible"}`}>Something</div>)}
</Sensor>
3reactions
MonliHcommented, Jan 2, 2020

I know this suggestion is quite old, but are there any plans to implement this? The Once prop would be one of those features that make this library better than others.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Once Upon Time Prop - Etsy
Check out our once upon time prop selection for the very best in unique or custom, handmade pieces from our shops.
Read more >
One Prop - Walmart
Shop for One Prop at Walmart.com. Save money. Live better. ... Funny He's a Wild One - Deer 1st Birthday Party Photo Booth...
Read more >
once upon a time prop for sale - eBay
Find great deals on eBay for once upon a time prop. Shop with confidence. ... ONCE UPON A TIME Evil Queen Heart Box...
Read more >
Render Props - React
A component with a render prop takes a function that returns a React ... that one component encapsulates to other components that need...
Read more >
react-prop-once - npm Package Health Analysis - Snyk
react-prop-once · Higher-order component to cache props once on initial mount, and propagate them on every other render For more information ...
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