将对象简写作为道具传递给React组件的方法是什么?

时间:2017-01-03 13:57:38

标签: reactjs jsx

将对象short-hand作为props传递给React组件以减少相同命名密钥对的冗余重复的方法是什么?

以下是我想避免的一些示例:

const Comp = ( {a, b, c, d} ) => {
    return (
        <div>
            <SubComp
                a={a}
                b={c}
                d={d}
            />
           <AnotherSubComp
                a={a}
                c={c}
            />
        </div>
    ) 
}; 

我想要的东西:

const Comp = ( {a, b, c, d} ) => {
    return (
        <div>
           <SubComp {a, c, d} />
           <AnotherSubComp {a, c} />
        </div>
    ) 
}; 

Spread运算符不是一个选项,创建一个中间对象根本不会减少额外的代码。

1 个答案:

答案 0 :(得分:5)

您可以像这样使用点差运算符:

test,test2
相关问题