Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Once prop #117

Open
GeeWizWow opened this issue May 18, 2018 · 9 comments
Open

Once prop #117

GeeWizWow opened this issue May 18, 2018 · 9 comments

Comments

@GeeWizWow
Copy link

GeeWizWow commented May 18, 2018

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

@GeeWizWow GeeWizWow changed the title Once prop Once prop May 18, 2018
@nirpeled
Copy link

+1

@fiso
Copy link

fiso commented Oct 31, 2018

For the record, you can very easily emulate this functionality by using the active prop, assuming you have your visibility-state stored.

  onChangeVisibility (isVisible) {
    this.setState({visible: isVisible});
  }

  render () {
      <VisibilitySensor onChange={this.onChangeVisibility} active={!this.state.visible} />
  }

@MonliH
Copy link

MonliH commented 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.

@omarryhan
Copy link

Someone made a PR for this: #143

@Hassene66
Copy link

Hassene66 commented Mar 31, 2020

For those who uses functional components

[State ,setState] = useState({visible: false });

const onChangeVisibility = isActive => {
    setState({ ...State, visible: isActive });
}
 <VisibilitySensor  onChange={e => onChangeVisibility(e)} active={!State.visible} />

@robbertvancaem
Copy link

robbertvancaem commented 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>

@MonliH
Copy link

MonliH commented Jul 17, 2020

If you really need this functionality and don't want to write some extra code yourself, you could also try react-on-screen, which seems to support the feature.

@andreabreu-me
Copy link

andreabreu-me commented Sep 3, 2020

I just want to share my solution. It would be great if this library has the hasBeenVisible render prop out of the box.

import React, { useState } from "react";
import VisibilitySensor from "react-visibility-sensor";

const AppearSensor = ({
  onChange,
  children,
  ...rest
}) => {
  const [hasBeenVisible, setHasBeenVisible] = useState(false);

  return (
    <VisibilitySensor {...rest} onChange={(isVisible) => {
      if (isVisible) setHasBeenVisible(true)
      if (onChange) onChange(isVisible)
    }}>
      {
        ({
          isVisible,
          ...restRenderProps
        }) => {
          return children({ isVisible, ...restRenderProps, hasBeenVisible })
        }
      }
    </VisibilitySensor>
  );
};

AppearSensor.propTypes = VisibilitySensor.propTypes
AppearSensor.defaultProps = VisibilitySensor.defaultProps

export default AppearSensor;

@katsos
Copy link

katsos commented Dec 8, 2020

This missing feature is the only one made me go with react-on-screen. If you need a contribution feel free to reach me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants