[英]react parent components passing child a prop with different name
Sorry for the terrible question name, I'm wondering if there is a standard way of passing a different value for the same prop from different parents and if there's something wrong with the way I am spreading my props.抱歉这个可怕的问题名称,我想知道是否有一种标准方法可以为来自不同父母的同一个道具传递不同的值,以及我传播道具的方式是否有问题。
Parent 1:家长 1:
<ChildComponent
prop1={prop1}
prop2={prop2}
paymentProcessingDate={Date.now()}
...passThroughProps
>
Parent 2 which has a prop carServiceDate具有道具 carServiceDate 的父 2
<ChildComponent
prop1={prop1}
prop2={prop2}
paymentProcessingDate={carServiceAppointment.date}
...passThroughProps
>
Both Parent1 and Parent2 make use of ChildComponent but need to pass in a different value for paymentProcessingDate. Parent1 和 Parent2 都使用 ChildComponent,但需要为 paymentProcessingDate 传入不同的值。 I'm receiving an error that the spread of passThroughProps will always overwrite paymentProcessingDate but the first time paymentProcessingDate is used or defined is in the call to ChildComponent.
我收到一个错误,即 passThroughProps 的传播将始终覆盖 paymentProcessingDate 但第一次使用或定义 paymentProcessingDate 是在对 ChildComponent 的调用中。 Any help or suggestions is really appreciated!!
非常感谢任何帮助或建议!
The ...passThroughProps
must contain a value for paymentProcessingDate
so TypeScript is complaining that your spread prop will overwrite whatever you explicitly set. ...passThroughProps
必须包含paymentProcessingDate
的值,因此 TypeScript 抱怨您的 spread prop 将覆盖您明确设置的任何内容。
Try moving the spread to the top and see if that fixes it:尝试将点差移到顶部,看看是否可以解决:
<ChildComponent
...passThroughProps
prop1={prop1}
prop2={prop2}
paymentProcessingDate={carServiceAppointment.date}
>
Or if possible, remove paymentProcessingDate
from passThroughProps
.或者,如果可能,从
passThroughProps
paymentProcessingDate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.