Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit df2a657

Browse files
authored
fix(handleRef): handle null ref correctly (#487)
1 parent 9bf84c1 commit df2a657

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/lib/handleRef.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const handleRef = <N>(ref: React.Ref<N>, node: N) => {
2020
return
2121
}
2222

23-
if (typeof ref === 'object') {
23+
if (ref !== null && typeof ref === 'object') {
2424
// @ts-ignore The `current` property is defined as readonly, however it's a valid way because
2525
// `ref` is a mutable object
2626
ref.current = node

test/specs/lib/handleRef-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ describe('handleRef', () => {
1818
expect(ref).toBeCalledWith(node)
1919
})
2020

21+
it('does not do anything when "ref" is null', () => {
22+
const node = document.createElement('div')
23+
24+
expect(() => {
25+
handleRef(null, node)
26+
}).not.toThrowError()
27+
})
28+
2129
it('assigns to "current" when "ref" is object', () => {
2230
const ref = React.createRef<HTMLDivElement>()
2331
const node = document.createElement('div')

0 commit comments

Comments
 (0)