Javascript 将元素属性道具的大型对象传递给组件
有没有一种简单的方法可以通过对象传递元素属性的“大”对象 例如:Javascript 将元素属性道具的大型对象传递给组件,javascript,html,reactjs,styled-components,Javascript,Html,Reactjs,Styled Components,有没有一种简单的方法可以通过对象传递元素属性的“大”对象 例如: const myAttrs = { one: "one", two: "two", ..., fifty: "fifty" }; return( <MyElement data={myAttrs}>Some label</MyElement> ); 使用spread运算符 <MyElement {...myAttrs}>Some label</M
const myAttrs = {
one: "one",
two: "two",
...,
fifty: "fifty"
};
return(
<MyElement data={myAttrs}>Some label</MyElement>
);
使用
spread
运算符
<MyElement {...myAttrs}>Some label</MyElement>
const MyElement = styled.a.attrs({one, two, three} => ({
// you can access these properties directly by using one for props.one and so on
一些标签
const MyElement=styled.a.attrs({1,2,3}=>({
//您可以通过使用props.one等直接访问这些属性
使用spread
运算符
<MyElement {...myAttrs}>Some label</MyElement>
const MyElement = styled.a.attrs({one, two, three} => ({
// you can access these properties directly by using one for props.one and so on
一些标签
const MyElement=styled.a.attrs({1,2,3}=>({
//您可以通过使用props.one等直接访问这些属性
将父母的大量道具传递给孩子
<Content
accounts={this.props.accounts}
showBalance={this.props.showBalance}
withdraw={this.props.withdraw}
deposit={this.props.deposit}
current={this.props.current}
depositAmount={this.props.depositAmount}
handleDeposit={this.props.handleDeposit}
handleRm={this.props.handleRm}
amounts={this.props.amounts}
getWithdraw={this.props.getWithdraw}
/>;
将父母的大量道具传递给孩子
<Content
accounts={this.props.accounts}
showBalance={this.props.showBalance}
withdraw={this.props.withdraw}
deposit={this.props.deposit}
current={this.props.current}
depositAmount={this.props.depositAmount}
handleDeposit={this.props.handleDeposit}
handleRm={this.props.handleRm}
amounts={this.props.amounts}
getWithdraw={this.props.getWithdraw}
/>;
在@kooskoos所做的扩展工作的基础上,我找到了一种方法,我认为我可以包含我喜欢的所有属性 基本上,我们在HTML中包含扩展对象
<MyElement {...myAttrs}>Label</MyElement>
这将通过对象中定义的所有道具,并按照您的定义将它们附加到HTML元素
<MyElement one="one" two="two" three="three" ...>Label</MyElement>
标签
在@kooskoos所做的扩展工作的背后,我找到了一种方法,我认为我可以包含我喜欢的所有属性
基本上,我们在HTML中包含扩展对象
<MyElement {...myAttrs}>Label</MyElement>
这将通过对象中定义的所有道具,并按照您的定义将它们附加到HTML元素
<MyElement one="one" two="two" three="three" ...>Label</MyElement>
标签
Crikey,这将是一个漫长的过程!我明白你的意思。也许按照@kooskoos的建议更合适,传播数据。@physicsboy请看我编辑的上述代码。我希望它能满足你的要求。Crikey,这将是一个漫长的过程!我明白你的意思。呃aps按照@kooskoos的建议做一些事情会更合适,传播数据。@physicsboy请查看我编辑过的上述代码。我希望它可以完全满足您的要求。然后如何在定义中访问它?myAttrs.one将等同于props.one,因此,您不能直接设置父v变量,所以要设置它们,您必须将setter方法作为propsSo传递。没有简单的方法可以省去像{one:props.one,two:props.two,…}这样的步骤
?在您的spread工作的背面,我找到了一种方法,我认为我可以包含我喜欢的所有属性。基本上我们可以按照您的建议执行:标签
,但是在常量
中,我们可以轻松访问,而不是单独突出显示属性,我们可以简单地编写导出常量MyElement=styled.a.attrs(道具=>道具)`…`
这将通过对象中定义的所有props,并按照您定义的方式将它们附加到HTML元素。然后如何在定义中访问它?myAttrs.one将等效于props.one等。在react中,您不能直接从child设置父变量,因此要设置它们,您必须传递setter方法作为propsSo,没有简单的方法可以省去像{one:props.one,two:props.two,…}
?在您的spread工作的背面,我找到了一种方法,我认为我可以包含我喜欢的所有属性。基本上我们可以按照您的建议执行:标签
,但是在常量
中,我们可以轻松访问,而不是单独突出显示属性,我们可以简单地编写导出常量MyElement=styled.a.attrs(props=>props)`…`
这将通过对象中定义的所有props,并按照您定义的方式将它们附加到HTML元素。