question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mismatch in type definitions of ref prop and useRef hook

See original GitHub issue

Do you want to request a feature or report a bug?

Bug?

What is the current behavior?

With Typescript strict mode turned on:

import React, { useRef } from 'react';
function Img() {
  const ref = useRef<HTMLImageElement>();
  return <img ref={ref} src="foobar" />
//            ^^^ The type check error below
}
Type 'MutableRefObject<HTMLImageElement | undefined>' is not assignable to type 'string | ((instance: HTMLImageElement | null) => void) | RefObject<HTMLImageElement> | null | undefined'.
  Type 'MutableRefObject<HTMLImageElement | undefined>' is not assignable to type 'RefObject<HTMLImageElement>'.
    Types of property 'current' are incompatible.
      Type 'HTMLImageElement | undefined' is not assignable to type 'HTMLImageElement | null'.
        Type 'undefined' is not assignable to type 'HTMLImageElement | null'.

The expected type comes from property 'ref' which is declared here on type 'DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>'

What is the expected behavior?

I would expect an empty ref like that to be acceptable, since (from what I can see) it works fine. But Typescript complains that useRef returns undefined while the ref prop expected null.

I can “fix” it by passing null as the “default ref”, like this:

const ref = useRef<HTMLImageElement>(null);

But unless there’s a reason why the ref prop expects null rather than undefined, I really think it would be better if for example the ref prop on HTMLElement was adjusted to match the default return from useRef, i.e. undefined? Yes? No? Or?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 16.12.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
bvaughncommented, Dec 18, 2019

But does React care whether or not it is undefined or null when passed as a prop to an HTML element?

I.e. will React treat these two differently?

No.

The key difference is what ref.current will be before the image has been set. The table below illustrates it:

before mount after mount after unmount
ImgNull null HTMLImageElement null
ImgUndefined undefined HTMLImageElement null

React sets host instance refs to null after unmount, so the TypeScript definition is trying to ensure that your ref.current is the same thing before mount as it is after unmount.

0reactions
Svishcommented, Dec 18, 2019

Aaaaah, that it makes sense. Thank you for the quick reply!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hooks: useRef, useImperativeHandle, & useLayoutEffect
This article continues our discussion of React's Additional Hooks. The… ... On initialization, useRef returns an object with a current property.
Read more >
Avoiding hydration mismatch when using React hooks
We follow the general rule that applies to all React hooks and components: A React hook or component MUST NOT return or render...
Read more >
Mutable and immutable useRef semantics with React ...
In this post, you will learn how different ways declaring a ref with useRef hook influence the immutability of the current ref property....
Read more >
React Hook Form Error - Type 'UseFormRegister<FormData ...
"Add a ref property to the input element in the Header component's JSX and set this to the register function from React Hook...
Read more >
React Refs: The Complete Story | Unicorn Utterances
React's useRef hook functions differently from useState , but they're ... If you look at the TypeScript definition for the hooks, you'll see ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found