将对象简写作为 props 传递给 React 组件的方法是什么?
What is the way to pass object short-hand as props into React component?
将对象简写作为 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>
)
};
展开运算符在这里不是一个选项,创建中间对象根本不会减少额外的代码。
你可以像这样使用展开运算符:
const Comp = ( {a, b, c, d} ) => {
return (
<div>
<SubComp {...{a, c, d}} />
<AnotherSubComp {...{a, c}} />
</div>
)
};
将对象简写作为 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>
)
};
展开运算符在这里不是一个选项,创建中间对象根本不会减少额外的代码。
你可以像这样使用展开运算符:
const Comp = ( {a, b, c, d} ) => {
return (
<div>
<SubComp {...{a, c, d}} />
<AnotherSubComp {...{a, c}} />
</div>
)
};