admin管理员组文章数量:1394593
I'm trying to find what to type React.createRef as. Header is a custom ponent. header.current always says
(property) React.RefObject.current: unknown Object is of type 'unknown'.
I can set it to type <any>
but that's the easy way out. How do I get Typescript to play nice and stop plaining.
Example: React.createRef<any>()
Fully code:
const header = React.createRef();
const { container } = render(
<Header{...props} headerRef={header} />,
);
const el = header.current.getButtonElement("button");
I've even tried setting it to a HTMLDivElement which is what it returns.
const header = React.createRef<HTMLDivElement>();
but the below still gives me an error: (property) React.RefObject.current: HTMLDivElement | null Object is possibly 'null'.
const el = header.current.getButtonElement("button");
I'm trying to find what to type React.createRef as. Header is a custom ponent. header.current always says
(property) React.RefObject.current: unknown Object is of type 'unknown'.
I can set it to type <any>
but that's the easy way out. How do I get Typescript to play nice and stop plaining.
Example: React.createRef<any>()
Fully code:
const header = React.createRef();
const { container } = render(
<Header{...props} headerRef={header} />,
);
const el = header.current.getButtonElement("button");
I've even tried setting it to a HTMLDivElement which is what it returns.
const header = React.createRef<HTMLDivElement>();
but the below still gives me an error: (property) React.RefObject.current: HTMLDivElement | null Object is possibly 'null'.
const el = header.current.getButtonElement("button");
Share
Improve this question
edited Sep 7, 2022 at 10:21
cccn
9491 gold badge8 silver badges20 bronze badges
asked Jun 4, 2020 at 20:14
me-meme-me
5,82914 gold badges62 silver badges115 bronze badges
2
-
Passing
HTMLDivElement
is correct, however the issue is, you can't callheader.current
directly afterwards, because the header.current can be null. It's only once the associated div element has mounted, willheader.current
have a reference to that element. Where are you usingconst el
? – user2340824 Commented Jun 4, 2020 at 21:08 - I'm calling it in a unit test. If I use <any> then ref works but when I use HTMLDivElement it doesn't – me-me Commented Jun 4, 2020 at 21:42
1 Answer
Reset to default 6This makes sense. TypeScript is trying to warn you that calling getButtonElement on something that MIGHT be undefined is "dangerous". You can work around this by refining the type first, like this:
const header = React.createRef<HTMLDivElement>();
// In test
const currentHeader = header.current;
if (!currentHeader) {
// Handle the case where header isn't set yet
}
currentHeader.getButtonElement("button"); // At this point TypeScript knows currentHeader can't be null so the type check passes
本文标签: javascriptReactcreateRef current type in typescriptStack Overflow
版权声明:本文标题:javascript - React.createRef current type in typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744096506a2590380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论