简体   繁体   English

添加唯一的数据属性以使用 webpack 来响应 dom 元素进行开发

[英]Add a unique data attribute to react dom elements using webpack for development

I am looking to add a certain data attribute to all my DOM elements on react for example例如,我希望在响应时向我的所有 DOM 元素添加某个数据属性

 <input
            hidden={this.props.hidden}
            // auto attribute
            data-testid={'uniqueAttribute45454'}
          />

I want this data-testid followed by a unique string for all my DOM elements (input, dropdown, checkbox) .我想要这个data-testid后跟一个唯一的字符串,用于我的所有 DOM 元素(输入、下拉、复选框)。 I want to do this via webpack so that I can segregate my webpack dev and webpack prod (I want this on dev only).我想通过 webpack 做到这一点,以便我可以隔离我的 webpack dev 和 webpack prod(我只希望在 dev 上这样做)。 Any webpack plugins available for this?任何可用的 webpack 插件? I have been researching for quite a while but haven't found anything.我已经研究了很长一段时间,但没有发现任何东西。

I've found a Babel plugin that you can use:我找到了一个可以使用的 Babel 插件:

https://github.com/shawnxusy/babel-plugin-react-generate-property https://github.com/shawnxusy/babel-plugin-react-generate-property

Readme says:自述文件说:

A plugin to automatically generate and add properties (for example data attributes) for all JSX open tags, especially styled components (Emotion, Styled Components, etc)一个插件,用于为所有 JSX 开放标签自动生成和添加属性(例如数据属性),尤其是样式组件(Emotion、样式组件等)

Exactly what I was looking for!正是我要找的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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