通过属性,但在React中例外

发布于 2025-02-04 09:05:51 字数 562 浏览 1 评论 0原文

有时我们都会做的常见。自定义组件中的wrap dom元素

< customcomponet id =“ abc” title =“ abc” nondomprop =“ abc” ... andsoforth/>

示例中的自定义组件button 具有属性ID标题但不是nondomprop,所以我得到警告是有道理的按钮 dom元素,我只是将所有道具传递给button dom element带有传播操作员< button {... props}/>

解决此问题的一种方法是手动仅通过按钮使用的道具,但是我想知道是否有一种方法告诉传播运营商使用所有传递的。 .props,但要忽略nondomprop

我试图进行Google搜索,但我找不到我想要的东西,所以我想也许我可以指向正确的方向。

There is something common that sometimes we all do. Wrap dom elements in custom components

<CustomComponet id="abc" title="abc" nonDomProp="abc" ...andsoforth />

Custom component in this example wraps button which has the properties id and title but not nonDomProp so I get a warning which makes sense because nonDomProp doesn't exist in the wrapped button DOM element and I am just passing all props to the button DOM element with the spread operator <button {...props} />

One way to solve this is by manually passing only the props that button will use, but I was wondering if there is a way to tell the spread operator to use all the passed ...props but to ignore nonDomProp.

I have tried to do a Google search and I have not been able to find what I am looking for so I thought maybe SO would point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2025-02-11 09:05:51

您可以使用以下方式破坏 props 对象:

const { nonDomProp, ...propsButton } = props;
return(
 <button {...propsButton} />
)

或直接来自 customcomponent 函数的参数:

const CustomComponent = ({ nonDomProp, ...props }) => {
...
return(
 <button {...props} />
)
}

docs: https://github.com/tc39/proposal-object--rest-spread-spread#rest-pread-properties

You can destructure the props object with this:

const { nonDomProp, ...propsButton } = props;
return(
 <button {...propsButton} />
)

Or directly from the argument of the CustomComponent function:

const CustomComponent = ({ nonDomProp, ...props }) => {
...
return(
 <button {...props} />
)
}

Docs: https://github.com/tc39/proposal-object-rest-spread#rest-properties

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文