From 8ede4bb10de53f6a999cb4a5a4c6f4c6edb9e50e Mon Sep 17 00:00:00 2001 From: olfedias Date: Mon, 19 Nov 2018 11:27:01 +0200 Subject: [PATCH] fix(handleRef): handle `null` ref correctly --- src/lib/handleRef.ts | 2 +- test/specs/lib/handleRef-test.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/handleRef.ts b/src/lib/handleRef.ts index 6fcea02285..f94086a3b8 100644 --- a/src/lib/handleRef.ts +++ b/src/lib/handleRef.ts @@ -20,7 +20,7 @@ const handleRef = (ref: React.Ref, node: N) => { return } - if (typeof ref === 'object') { + if (ref !== null && typeof ref === 'object') { // @ts-ignore The `current` property is defined as readonly, however it's a valid way because // `ref` is a mutable object ref.current = node diff --git a/test/specs/lib/handleRef-test.ts b/test/specs/lib/handleRef-test.ts index fc698353f5..d938ca5dfe 100644 --- a/test/specs/lib/handleRef-test.ts +++ b/test/specs/lib/handleRef-test.ts @@ -18,6 +18,14 @@ describe('handleRef', () => { expect(ref).toBeCalledWith(node) }) + it('does not do anything when "ref" is null', () => { + const node = document.createElement('div') + + expect(() => { + handleRef(null, node) + }).not.toThrowError() + }) + it('assigns to "current" when "ref" is object', () => { const ref = React.createRef() const node = document.createElement('div')