Skip to content

Commit 97e81a2

Browse files
committed
fix: broken hook updates
Hooks were comparing against the previous state.inView, without having access to that value. This resulted in hooks never triggering when outside the viewport.
1 parent daf56df commit 97e81a2

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"eslintIgnore": [
3939
"*.snap",
40+
"test-utils.js",
4041
"dist/**",
4142
"lib/**",
4243
"example/**",
@@ -77,15 +78,18 @@
7778
],
7879
"parser": "@typescript-eslint/parser",
7980
"plugins": [
80-
"@typescript-eslint"
81+
"@typescript-eslint",
82+
"react-hooks"
8183
],
8284
"settings": {
8385
"react": {
8486
"version": "detect"
8587
}
8688
},
8789
"rules": {
88-
"no-unused-vars": 0
90+
"no-unused-vars": 0,
91+
"react-hooks/rules-of-hooks": "error",
92+
"react-hooks/exhaustive-deps": "warn"
8993
}
9094
},
9195
"jest": {
@@ -138,6 +142,7 @@
138142
"eslint-plugin-import": "2.16.0",
139143
"eslint-plugin-jsx-a11y": "6.x",
140144
"eslint-plugin-react": "7.x",
145+
"eslint-plugin-react-hooks": "^1.5.0",
141146
"husky": "^1.3.1",
142147
"intersection-observer": "^0.5.1",
143148
"jest": "^24.5.0",

src/__tests__/hooks.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ test('should create a hook inView', () => {
5050
getByText('true')
5151
})
5252

53+
test('should trigger a hook leaving view', () => {
54+
const { getByText } = render(<HookComponent />)
55+
mockAllIsIntersecting(true)
56+
mockAllIsIntersecting(false)
57+
getByText('false')
58+
})
59+
5360
test('should respect trigger once', () => {
5461
const { getByText } = render(
5562
<HookComponent options={{ triggerOnce: true }} />,

src/test-utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ export function mockIsIntersecting(element: Element, isIntersecting: boolean) {
5555
{
5656
isIntersecting,
5757
target: element,
58-
intersectionRatio: isIntersecting ? 1 : -1,
58+
intersectionRatio: isIntersecting ? 1 : 0,
5959
},
6060
]
61-
6261
if (act) act(() => cb(entry))
6362
else cb(entry)
6463
} else {

src/useInView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
import * as React from 'react'
23
import { observe, unobserve } from './intersection'
34
import { HookResponse, IntersectionOptions } from './typings/types'

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,11 @@ [email protected]:
42524252
has "^1.0.3"
42534253
jsx-ast-utils "^2.0.1"
42544254

4255+
eslint-plugin-react-hooks@^1.5.0:
4256+
version "1.5.0"
4257+
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-1.5.0.tgz#cdd958cfff55bd5fa4f84db90d1490fb5ca4ae2b"
4258+
integrity sha512-iwDuWR2ReRgvJsNm8fXPtTKdg78IVQF8I4+am3ntztPf/+nPnWZfArFu6aXpaC75/iCYRrkqI8nPCYkxJstmpA==
4259+
42554260
42564261
version "7.12.4"
42574262
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.12.4.tgz#b1ecf26479d61aee650da612e425c53a99f48c8c"

0 commit comments

Comments
 (0)