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

fix(handleRef): handle null ref correctly #487

Merged
merged 2 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/handleRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const handleRef = <N>(ref: React.Ref<N>, 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
Expand Down
8 changes: 8 additions & 0 deletions test/specs/lib/handleRef-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>()
const node = document.createElement('div')
Expand Down