简体   繁体   中英

How to use Microdata in React+Typescript?

When I add itemscope itemtype="http://schema.org/Product" to h1 , I get this error:

Type '{ children: string; itemscope: true; itemtype: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'. Property 'itemscope' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'

<h1 itemscope itemtype="http://schema.org/Product">Amasia</h1>

How to use Microdata in React+Typescript?

Not very elegant, but this works:

// foo.d.ts
declare namespace React {
    interface HTMLAttributes<T> {
        itemscope?: boolean;
        itemtype?: string;
    }
}
// app.tsx
function foo(): JSX.Element {
    return (
        <h1 itemscope itemtype="http://schema.org/Product">
            Amasia
        </h1>
    );
}

Works for me (Typescript 3.4.5).

For React, it is case Sensitive. Pay attention to: itemScope and itemType spelling

<div itemScope itemType={"http://schema.org/Product"}>{...}</div>

如果你使用打字稿,你应该这样写

<h1 itemScope itemType={"http://schema.org/Product"}>{...}</h1>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM