Skip to content

Commit 5f9a950

Browse files
committed
fix(VO): check if entry exists
1 parent 340b452 commit 5f9a950

9 files changed

+29
-16
lines changed

.prettierrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"bracketSpacing": true,
33
"jsxBracketSameLine": false,
4-
"parser": "babylon",
54
"printWidth": 80,
65
"semi": false,
76
"singleQuote": true,

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"storybook": "start-storybook -p 9001 -c .storybook",
2323
"prepare": "npm run test && npm run test",
2424
"test": "jest",
25-
"test:dev": "jest --watch"
25+
"test:dev": "jest --watch",
26+
"format": "prettier --config .prettierrc src"
2627
},
2728
"repository": {
2829
"type": "git",
@@ -36,8 +37,8 @@
3637
"homepage": "https://github.com/lessp/react-is-visible",
3738
"husky": {
3839
"hooks": {
39-
"pre-commit": "npm test",
40-
"pre-push": "npm test"
40+
"pre-commit": "npm run format && npm test",
41+
"pre-push": "npm run format && npm test"
4142
}
4243
},
4344
"peerDependencies": {
@@ -72,6 +73,7 @@
7273
"intersection-observer": "0.5.1",
7374
"jest": "24.1.0",
7475
"jest-dom": "3.1.2",
76+
"prettier": "^2.0.5",
7577
"react": "^16.8.0",
7678
"react-dom": "^16.8.0",
7779
"react-testing-library": "5.9.0",

src/IsVisible.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class IsVisible extends React.PureComponent {
2727
}
2828

2929
if (isIntersecting && this.props.once) {
30-
this.unwatch()
30+
this.unwatch()
3131
}
3232
}
3333

src/VisibilityObserver.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ let intersectionObserver
22
let intersectionObserverOptions = {}
33
const subscribers = new WeakMap()
44

5-
const handleIntersections = entries =>
6-
entries.forEach(entry => subscribers.get(entry.target).call(null, entry))
5+
const handleIntersections = (entries) =>
6+
entries.forEach((entry) => {
7+
const maybeEntry = subscribers.get(entry.target)
8+
9+
if (maybeEntry) {
10+
maybeEntry.call(null, entry)
11+
}
12+
})
713

814
const getIntersectionObserver = () => {
915
if (!intersectionObserver) {
@@ -16,7 +22,7 @@ const getIntersectionObserver = () => {
1622
return intersectionObserver
1723
}
1824

19-
const setIntersectionObserverOptions = options => {
25+
const setIntersectionObserverOptions = (options) => {
2026
if (intersectionObserver) {
2127
return
2228
}
@@ -35,7 +41,7 @@ const watch = (domNode, callback) => {
3541
return () => unwatch(domNode)
3642
}
3743

38-
const unwatch = domNode => {
44+
const unwatch = (domNode) => {
3945
intersectionObserver.unobserve(domNode)
4046
subscribers.delete(domNode)
4147
}

src/__tests__/hook.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useIsVisible } from '../'
66

77
afterEach(cleanup)
88

9-
const FunctionalComponent = props => {
9+
const FunctionalComponent = (props) => {
1010
const nodeRef = useRef()
1111
const isVisible = useIsVisible(nodeRef)
1212

src/__tests__/vo.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ afterEach(cleanup)
88

99
const FunctionalComponent = ({ isVisible, ...props }) => <h1 {...props} />
1010
const FunctionalComponentWithIsVisible = withIsVisible(FunctionalComponent)
11-
const FunctionalComponentHook = props => {
11+
const FunctionalComponentHook = (props) => {
1212
const nodeRef = useRef()
1313
const isVisible = useIsVisible(nodeRef)
1414

src/useIsVisible.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import VO from './VisibilityObserver'
44

55
const defaultOptions = {
66
once: false,
7-
};
7+
}
88

99
function useIsVisible(nodeRef, { once } = defaultOptions) {
1010
const [isVisible, setVisible] = useState(false)

src/withIsVisible.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import hoistNonReactStatic from 'hoist-non-react-statics'
44

55
import IsVisible from './IsVisible'
66

7-
const getDisplayName = WrappedComponent =>
7+
const getDisplayName = (WrappedComponent) =>
88
WrappedComponent.displayName || WrappedComponent.name || 'Component'
99

1010
const defaultOptions = {
1111
once: false,
12-
};
12+
}
1313

14-
export const withIsVisible = (WrappedComponent, { once } = defaultOptions)=> {
14+
export const withIsVisible = (WrappedComponent, { once } = defaultOptions) => {
1515
const WithIsVisible = ({ forwardedRef, ...props }) => (
1616
<IsVisible
1717
once={once}
18-
children={isVisible => (
18+
children={(isVisible) => (
1919
<WrappedComponent {...props} isVisible={isVisible} ref={forwardedRef} />
2020
)}
2121
/>

0 commit comments

Comments
 (0)