简体   繁体   English

反应父组件向子组件传递具有不同名称的道具

[英]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.

相关问题 使用React和Gatsby将道具从父母传递给孩子 - Passing a prop to a child from parent with React and Gatsby 从父组件传递道具在子组件 React 中变为空 - Passing a prop from a Parent component becomes null in the Child component React React - 父组件将子组件呈现给不同的元素/节点 - React - parent components render child components to different elements/nodes 通过从子组件到父组件的函数来反应Redux传递参数 - React Redux passing parameters via functions from child components to parent 在没有子父关系的 React 组件之间传递数据? - Passing data between React components with no child-parent relationship? React 将 function 作为 prop 传递给子组件,但父组件未访问子组件提供的数据 - React Passing a function as prop to child component, but the parent is not accessing the data provided by the child 将 prop 从父组件传递给子组件时出错 - Error with passing a prop from parent to child component 通过React JS中具有多个组件的父级传递和更新prop / state值 - Passing and updating prop / state values from parent with multiple components in React JS ReactJS,将prop从父组件传递到子组件? - ReactJS, passing prop from parent to child component? 限制 prop 传递到 React 组件 - Limiting prop-passing into React components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM