[英]How can I create a wrapper component and pass down props to call it in the parent of the wrapper
I have this component: 我有这个组成部分:
import React from 'react';
import PropTypes from 'prop-types';
const TableExpandedRowItem = ({ dataTitle, rowValue, cellClassProp }) => (
<div data-title={dataTitle} className={cellClassProp}>
<p>{rowValue}</p>
</div>
);
TableExpandedRowItem.propTypes = {
dataTitle: PropTypes.string.isRequired,
rowValue: PropTypes.string.isRequired,
cellClassProp: PropTypes.string,
};
TableExpandedRowItem.defaultProps = {
cellClassProp: 'cell',
};
export default TableExpandedRowItem;
Which works perfect when I call it on the parent, like this: 当我在父级上调用它时,这种方法非常完美,如下所示:
{rowExpanded.billingItems &&
rowExpanded.billingItems.map(
item =>
rowExpanded.id ===
item.cancellationRequestId && (
<div className="row" key={item.id}>
<TableExpandedRowItem
dataTitle={
item.billingItem.description
}
rowValue={
item.billingItem.description
}
/>
<TableExpandedRowItem
dataTitle={
item.billingItem.recurringFee
}
rowValue={
item.billingItem.recurringFee
}
/>
</div>
),
)}
But I want to create a wrapper component. 但是我想创建一个包装器组件。 if you see the code above, there is a div
wrapping the calls to <TableExpandedRowItem/>
component. 如果您看到上面的代码,则将div
包装到<TableExpandedRowItem/>
组件的调用中。
So I want to do something like this: 所以我想做这样的事情:
import TableExpandedRowItem from './TableExpandedRowItem'
const TableExpandedRowWrapper = ({ rowClassProp }) => (
<div className={rowClassProp}>
<TableExpandedRowItem dataTitle={}/>
</div>
);
But if you see in the second code snippet, I am calling it twice with different data. 但是,如果您在第二个代码段中看到的话,我会用不同的数据两次调用它。 How can I replicate that in the wrapper component instead? 我该如何在包装器组件中复制呢?
Not really sure if this is what you want to achieve but just put your keys into a map and do another iteration. 不太确定这是否是您要实现的,只是将密钥放入地图中并进行另一次迭代。
Note: Code is not tested and not optimized for avoiding unnecessary render cycles. 注意:未对代码进行测试且未针对避免不必要的渲染周期进行优化。
{rowExpanded.billingItems &&
rowExpanded.billingItems.map(
item =>
rowExpanded.id === item.cancellationRequestId && (
<TableExpandedRowWrapper billingItem={item.billingItem} />
),
)}
Your wrapper 你的包装
const TableExpandedRowWrapper = ({ billingItem }) => (
<div className="row">
// alternatively iterate over Object.values(billingItem)
['description', 'recurringFee'].map(key) =>
<TableExpandedRowItem
dataTitle={
billingItem[key].description
}
rowValue={
billingItem[key].description
}
/>
)
</div>
);
Sidenote: it seems a bit strange that in your example the rowValue
and your dataTitle
are both description
... 旁注:在您的示例中, rowValue
和dataTitle
都是description
...似乎有点奇怪...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.