如何将对象简写作为 props 传递到 React 组件中以减少相同命名 key 对的冗余重复?
以下是我想避免的事情的示例:
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>
)
};
扩展运算符在这里不是一个选项,并且创建中间对象根本不会减少额外的代码。
最佳答案
您可以在此处使用展开运算符,如下所示:
const Comp = ( {a, b, c, d} ) => {
return (
<div>
<SubComp {...{a, c, d}} />
<AnotherSubComp {...{a, c}} />
</div>
)
};
关于reactjs - 将对象简写作为 props 传递到 React 组件中的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41445186/