简体   繁体   English

如何在没有任何值的情况下向 JSX 中的 HTML 元素添加数据属性

[英]How to add data attribute to HTML element in JSX without any value

At first I had to add an attribute to a custom element/component, so I tried doing it here, but i can only add it to non-custom elements like div and input.起初我不得不向自定义元素/组件添加一个属性,所以我在这里尝试这样做,但我只能将它添加到非自定义元素,如 div 和 input。

https://codesandbox.io/s/react-16-966eq https://codesandbox.io/s/react-16-966eq

const dAttribute = { "data-rr-mask": "" };
const App = () => (
  <div style={styles}>
    <h4>Context </h4>
    <FeatureContext {...dAttribute} name="Context" />
    <hr />
    <h4> Error2 Boundaries</h4>
    <FeatureErrorBoundaries />
    <hr />
    <h4> Fragment</h4>
    <h2>
      <FeatureFragment />
    </h2>
    <hr />
    <h4>Reference</h4>
    <FeatureRef />
    <hr />
    <h4>Portals</h4>
    <FeaturePortal />
    <hr />
    <h4>Life Cycle</h4>
    <FeatureLifeCycle />
  </div>
);

render(<App {...dAttribute} />, document.getElementById("root"));

Also, this attribute will finally be used on an HTML element and I need it to be without any value, eg: <input data-rr-mask /> and another issue is that I have to add an empty string as value;此外,此属性最终将用于 HTML 元素,我需要它没有任何值,例如: <input data-rr-mask />另一个问题是我必须添加一个空字符串作为值; otherwise, the application throws an error.否则,应用程序会抛出错误。

Is there a way to do it so that you don't get "data-rr-mask"='' inside the element有没有办法做到这一点,这样你就不会在元素内得到"data-rr-mask"=''

EDIT (based on comments, OP is actually looking for data attributes to be resolved without any value, on the generated HTML)编辑(基于评论,OP 实际上是在生成的 HTML 上寻找要解析的数据属性,没有任何值)

In order to eliminate the value entirely for the generated HTML element, as a user commented on my answer, you just need to use an empty string for your data-attribute/prop.为了完全消除生成的 HTML 元素的值,正如用户对我的回答所评论的那样,您只需要为您的数据属性/道具使用一个空字符串。

export default function App() {
  return (
    <div className="App">
      <input data-rr-mask="" /> {/* will be resolved to <input data-rr-mask> on the generated HTML code*/} 
    </div>
  );
}

Here is a working example of it.这是它的一个工作示例。

Here is a working example mixing both your requests.这是一个混合您的请求的工作示例


(Original answer based on first request) (基于第一个请求的原始答案)

At first you must consider that every attribute you pass to a custom component, can then be found on components props .首先,您必须考虑传递给自定义组件的每个属性,然后都可以在组件props上找到。

So for example, if you pass a custom attribute on your App component named data-rr-mask you can then found it, under your App 's props:因此,例如,如果您在名为data-rr-mask App组件上传递自定义属性,则可以在App的道具下找到它:

const App = (props) => {
  console.log(props) // { "data-rr-mask": "foo" }

  return <div>
    <MySecondCustomComponent {...props} /> 
  </div>
};


render(<App data-rr-mask="foo" />, document.getElementById("root"));

And then again, in your MySecondCustomComponent under its props you can find your custom attribute.然后,在你的MySecondCustomComponentprops你可以找到你的自定义属性。

In my understanding a prop without a value is a boolean prop with value true.在我的理解中,一个没有值的道具是一个值为真的布尔道具。

So that means所以这意味着

<div someProp />

is the same as是相同的

<div someProp={true} />

Try if the following solves your problem:尝试以下操作是否能解决您的问题:

const dAttribute = { "data-rr-mask": true };

See https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes for more information on boolean HTML attributes.有关布尔 HTML 属性的更多信息,请参阅https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes

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

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