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.

[typescript] Define components as ComponentClass, not ComponentType

See original GitHub issue

See e.g. Button: https://github.com/mui-org/material-ui/blob/b29f1cbec31548291a75959d3627473569343eb4/src/Button/Button.d.ts#L36.

ComponentClass supports ref= whereas ComponentType doesn’t (because the component may be a stateless functional component). So any attempt to use ref= results in a Typescript compile error.

ref’s are often used to find the anchor element for popovers and similar.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
pelotomcommented, Mar 18, 2018

For most Material UI components, the argument to a ref={...} callback would be the withStyles HOC instance… probably not what you want. It seems like a leakage of implementation details to expose that, hence why we use ComponentType everywhere, and instead expose targeted fooRef props where appropriate.

0reactions
estaubcommented, Mar 18, 2018

Ready to close, by me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[typescript] Define components as ComponentClass, not ...
ComponentClass supports ref= whereas ComponentType doesn't (because the component may be a stateless functional component). So any attempt to ...
Read more >
What is the Typescript type to accept any kind of React ...
React.ComponentType<P> is the type for either a class component ( React.ComponentClass<P> ) or function component ( React.
Read more >
Class Components - React TypeScript Cheatsheets
Within TypeScript, React.Component is a generic type (aka React.Component), so you want to provide it with (optional) prop and state type parameters:
Read more >
How to Statically Type React Components with TypeScript
In this guide, you will learn how to statically type React.js components using the compile-to-JavaScript language, TypeScript.
Read more >
React Higher-Order Components in TypeScript - Medium
Note that the examples in this article do not demonstrate best practices, ... Here we are defining a component to return from the...
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