最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Typescript element types - Stack Overflow

programmeradmin3浏览0评论

What's the type of the following statement in React?

let element = <div>Blah blah</div>

I thought I could use element: HTMLElement but when I do, I get Property 'accessKey' is missing in type 'Element'.

What about another imported ponent?

import Row from './partials/Row';
let row = <Row cols={cols} />

What's the type of the following statement in React?

let element = <div>Blah blah</div>

I thought I could use element: HTMLElement but when I do, I get Property 'accessKey' is missing in type 'Element'.

What about another imported ponent?

import Row from './partials/Row';
let row = <Row cols={cols} />
Share Improve this question edited Dec 26, 2016 at 0:25 Nitzan Tomer 164k51 gold badges328 silver badges306 bronze badges asked Dec 25, 2016 at 23:12 KoushaKousha 36.2k59 gold badges186 silver badges313 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The type for the div jsx element is HTMLFactory<HTMLDivElement>.

The reference you have isn't for an actual dom element in the browser but for a react element which represent such an element which might not be rendered yet.

The html properties should be accessed through the props:

let element = <div>Blah blah</div>
console.log(element.props.accessKey);

If you'd like to get the dom element you can use the ref attribute:

When the ref attribute is used on an HTML element, the ref callback receives the underlying DOM element as its argument

So:

let divElement: HTMLDivElement;
let element = <div ref={ (el) => { divElement = el; console.log(el); } }>Blah blah</div>

In the case of the imported Row, again, you have a reference to a react element which has a dom representation.
Which properties or methods it has depends on the class itself.

发布评论

评论列表(0)

  1. 暂无评论