将对象作为道具传递给jsx [英] Passing object as props to jsx
本文介绍了将对象作为道具传递给jsx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象,其中包含多个通用键值道具,我想将它们传递给一些jsx.像这样:
I have an object contains multiple common key-value props, that I want to pass on to some jsx. Something like this:
const commonProps = {myProp1: 'prop1',myProp2: 'prop2'};
<MyJsx commonProps />
我希望它可以作为传递单个道具的功能:
I want this to function as passing individual props:
<MyJsx myProp1={commonProps.myProp1} myProp2={commonProps.myProp2}/>
这可能吗?
推荐答案
这可能吗?
Is this possible?
是的,为什么您认为不可能,但是发送方式不正确.
Yes, why you think its not possible, but the way you are sending is not correct.
<MyJsx commonProps />
的含义是:
<MyJsx commonProps={true} />
因此,如果您在默认情况下未指定任何值,它将采用true
.要传递对象,您需要这样写:
So if you don't specify any value by default it will take true
. To pass the object you need to write it like this:
const commonProps = {myProp1: 'prop1',myProp2: 'prop2'};
<MyJsx commonProps={commonProps} />
更新:
如果您有一个对象,并且希望将所有属性作为单独的prop传递,请这样编写:
If you have an object and wants to pass all the properties as separate prop, write it like this:
<MyJsx {...commonProps} />
这篇关于将对象作为道具传递给jsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文