-
Notifications
You must be signed in to change notification settings - Fork 196
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
Comments
+1 |
For the record, you can very easily emulate this functionality by using the onChangeVisibility (isVisible) {
this.setState({visible: isVisible});
}
render () {
<VisibilitySensor onChange={this.onChangeVisibility} active={!this.state.visible} />
} |
I know this suggestion is quite old, but are there any plans to implement this? The |
Someone made a PR for this: #143 |
For those who uses functional components
|
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> |
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. |
I just want to share my solution. It would be great if this library has the 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; |
This missing feature is the only one made me go with react-on-screen. If you need a contribution feel free to reach me. |
Any chance of getting a
once
prop, that would only trigger the event once ?The text was updated successfully, but these errors were encountered: