Search⌘ K
AI Features

Quiz

Evaluate your understanding of creating strongly typed refs in React components with TypeScript. This quiz reinforces key concepts covered in the lesson to help you apply strong typing in your React projects effectively.

We'll cover the following...

Creating strongly-typed refs

1.

We want to get a reference to a div element in a class component and have made an attempt at this below:

class SomeComponent extends React.Component {
  private div = React.createRef<HTMLSpanElement>();

  ...
  
  render() {
    return <div ref={this.div}>...</div>;
  }
}

What is the problem with this code?

A.

You can’t use createRef in class components.

B.

There is no need to pass the type to createRef because TypeScript will infer it correctly.

C.

The wrong type has been passed into createRef. The type should be HTMLDivElement.


1 / 3

Well done!

We are going to ...