通过属性,但在React中例外
有时我们都会做的常见。自定义组件中的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下方式破坏 props 对象:
或直接来自 customcomponent 函数的参数:
docs: https://github.com/tc39/proposal-object--rest-spread-spread#rest-pread-properties
You can destructure the props object with this:
Or directly from the argument of the CustomComponent function:
Docs: https://github.com/tc39/proposal-object-rest-spread#rest-properties