Quiz
This quiz will test your knowledge on working with React refs with TypeScript.
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
.
Question 1 of 30 attempted
Get hands-on with 1400+ tech skills courses.