使用道具和其他道具
序列中只需添加:
<AccountsStatus {...props} otherProp={true} />
或者你也可以复制道具,加其他道具克隆,然后执行:
<AccountsStatus {...newProps} />
但我不建议做,你会东东一个lib克隆的对象。
我尝试了第一次,但由于某种原因不起作用 – PositiveGuy
您可以发布您的代码@PositiveGuy吗? –
如果'props'里面'otherProp'的值已经存在,恐怕我不得不使用第二种方法。 –
做到这一点更简洁的方法是基于使用an object spread operator:
<AccountsStatus {...props, extraProp: extraValue} />
为了能够使用它,你需要通天配置为使用transform-object-rest-spread插件。
请记住,您传递道具的顺序将决定将哪个值传递给该组件。这适用于有两个相同名称的密钥。
例如:
<AccountsStatus extraProp={extraValue} {...props} />
和
<AccountsStatus {...props} extraProp={extraValue} />
看起来一样,但在第一个例子,如果你传播了载有extraProp
关键的props
对象时,它会覆盖您之前通过的extraProp
值。
只需发送道具传播员上方:
<AccountsStatus
otherProp={otherPropValue}
{...props}
/>
记住始终指定它上面的传播道具。如果您在道具中传递道具名称相同的道具,您可能想要覆盖它。 您可以在this article中阅读更多关于道具的文章,它涵盖了很多道具如何通过的方法。
我现在不能测试这个,但不应该这项工作?
<AccountsStatus foo={[...props, extraProp]} />
'{... props,extraProp:someValue}'? – raina77ow
是的它的工作原理,我试过,发布之前,得到一个错误,但没关系。 – PositiveGuy
我应该发布这个答案吗? – raina77ow