How to add custom html attributes in JSX

后端 未结 11 1922
南方客
南方客 2020-11-27 18:14

There are different reasons behind it, but I wonder how to simply add custom attributes to an element in JSX?

11条回答
  •  感情败类
    2020-11-27 18:53

    I ran into this problem a lot when attempting to use SVG with react.

    I ended up using quite a dirty fix, but it's useful to know this option existed. Below I allow the use of the vector-effect attribute on SVG elements.

    import SVGDOMPropertyConfig from 'react/lib/SVGDOMPropertyConfig.js';
    import DOMProperty from 'react/lib/DOMProperty.js';
    
    SVGDOMPropertyConfig.Properties.vectorEffect = DOMProperty.injection.MUST_USE_ATTRIBUTE;
    SVGDOMPropertyConfig.DOMAttributeNames.vectorEffect = 'vector-effect';
    

    As long as this is included/imported before you start using react, it should work.

提交回复
热议问题