True custom attributes (e.g. Microdata) in React

后端 未结 5 1784
渐次进展
渐次进展 2020-12-09 16:57

The site I am developing makes use of Microdata (using schema.org). As we are shifting development over to use React to render our views I have hit a blocker where React wil

相关标签:
5条回答
  • 2020-12-09 17:05

    For those who's still looking for answers: https://facebook.github.io/react/docs/tags-and-attributes.html

    Example:

    <div itemScope itemType="http://schema.org/Article"></div>
    
    0 讨论(0)
  • 2020-12-09 17:07

    You should be able to do it with componentDidMount:

    ...
    componentDidMount: function() {
      if (this.props.itemtype) {
        this.getDOMNode().setAttribute('itemscope', true)
        this.getDOMNode().setAttribute('itemtype', this.props.itemtype)
      }
    
      if (this.props.itemprop) {
        this.getDOMNode().setAttribute('itemprop', this.props.itemprop)
      }
    }
    ...
    

    The whole check for Microdata attributes can be wrapped into a mixin for convenient. The problem with this approach is that it won't work for built-in React component (components created by React.DOM). Update: Looking closer at React.DOM, I come up with this http://plnkr.co/edit/UjXSveVHdj8T3xnyhmKb?p=preview. Basically we wrap the built-in components in a custom component with our mixin. Since your components are built upon React 's built-in DOM components, this would work without you having to include the mixin in the components.

    The real solution would be injecting a custom config instead of React's DefaultDOMPropertyConfig, however I can't find a way to do so in a drop-in manner (DOMProperty is hidden by the module system).

    0 讨论(0)
  • 2020-12-09 17:18

    You can also use "is" attribute. It will disable the attribute white-list of React and allow every attribute. But you have to write class instead of className and for instead of htmlFor if you use is.

    <div is my-custom-attribute="here" class="instead-of-className"></div>
    

    Update React 16 custom attributes are now possible

    In react 16 custom attributes are now possible

    React 16 custom attributes

    0 讨论(0)
  • 2020-12-09 17:20

    So far, the best method I've found is based off of some Amp interop code linked from a comment on react's bug tracker thread on the subject. I modified it slightly to work with a newer version of React (15.5.4) and TypeScript.

    For regular ES6, you can just remove the type annotation for attributeName. Using require was needed in TS since DOMProperty isn't exposed in react's index.d.ts, but again import could be used in regular ES6.

    // tslint:disable-next-line:no-var-requires
    const DOMProperty = require("react-dom/lib/DOMProperty");
    if (typeof DOMProperty.properties.zz === "undefined") {
        DOMProperty.injection.injectDOMPropertyConfig({
            Properties: { zz: DOMProperty.MUST_USE_ATTRIBUTE },
            isCustomAttribute: (attributeName: string) => attributeName.startsWith("zz-")
        });
    }
    

    Now you can use any attribute starting with zz-

    <div zz-context="foo" />
    

    Normally it'd be a bad idea to use internal parts of react like this, but I think it is better than any of the other methods. It works the same way as existing open-ended attributes like data- and the JSX is even type safe in TS. I believe the next major version of react is going to do away with the whitelist anyway, so hopefully changes won't be needed before we can remove this shim entirely.

    0 讨论(0)
  • 2020-12-09 17:22

    It looks like these non-standard properties have been added to React

    itemProp: MUST_USE_ATTRIBUTE, // Microdata: http://schema.org/docs/gs.html
    itemScope: MUST_USE_ATTRIBUTE | HAS_BOOLEAN_VALUE, // Microdata: http://schema.org/docs/gs.html
    itemType: MUST_USE_ATTRIBUTE, // Microdata: http://schema.org/docs/gs.html
    

    Note that properties have capital letter in the middle:

    <div itemProp="whatever..." itemScope itemType="http://schema.org/Offer">
    

    will generate proper lowercase attributes as result.

    0 讨论(0)
提交回复
热议问题