gpt4 book ai didi

reactjs - 在 Material-Table 渲染属性中使用函数

转载 作者:行者123 更新时间:2023-12-03 08:46:25 25 4
gpt4 key购买 nike

我需要在 Material-Table 列渲染属性中使用自定义函数。该函数被调用,我在控制台上打印了预期的结果,但是,结果根本不会在表中呈现。这是代码:

import React from 'react';
import HraReferenceDataContext from '../context/hraReferenceData/hraReferenceDataContext';
import MaterialTable from 'material-table';

const EmployeeDetailsCompanyDocuments = ({ companyDocumentsData }) => {
const hraReferenceDataContext = React.useContext(HraReferenceDataContext);
const { companyDocumentTypes } = hraReferenceDataContext;

const getDocumentTypeForRow = id => {
companyDocumentTypes.forEach(type => {
if (type.id === id) {
console.log(type.name)
return type.name;
}
});
};

const columnInfo = [
{
field: 'typeId',
title: 'Type',
render: rowData =>{ getDocumentTypeForRow(rowData.typeId)}, //here is the problem
},
{ field: 'created', title: 'Created On' },

];

return (
<MaterialTable
columns={columnInfo}
data={companyDocumentsData}
title="Company Documents List"
/>
);
};

最佳答案

forEach 内部返回不起作用。

更改此功能

const getDocumentTypeForRow = id => {
companyDocumentTypes.forEach(type => {
if (type.id === id) {
console.log(type.name)
return type.name;
}
});
};

const getDocumentTypeForRow = id => {
return companyDocumentTypes.find(type => type.id === id).name;
};

更新

改变

render: rowData =>{ getDocumentTypeForRow(rowData.typeId)},

render: rowData => getDocumentTypeForRow(rowData.typeId),

因为您应该返回从 getDocumentTypeForRow 返回的值。

关于reactjs - 在 Material-Table 渲染属性中使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61287868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com