React中Ant Design的Tabs组件如何使用items属性动态配置Tab,并在过滤时重置子组件数据?
react中使用antd的Tabs,目前用的antd是5.17.0,因为这个版本的Tabs只能使用items来配置Tab,之前4.x.x的版本是用的TabPane。下面是Tabs的items,children是对应子组件。现在是通过useState对detailTabList里面的对象进行隐藏,根据其他的值来设置visiable,然后用filte过滤那些Tab不显示。因为这个导致children子组件{oDat}对象的值不会变(对象有多个属性),oDat也是放在useState中,有什么办法在过滤detailTabList时,重新设置children中oDat的值吗?
let detailTabList = [
{
label: 'test1',
key: '0',
visiable: true,
children: <RequestInfo oDat={oDat} iAppMod={state.type} ChangeRequestInfo={(v: any) => changeODat(v)}/>,
},
{
label: 'test2',
key: '1',
visiable: true,
children: <ResponseInfo oDat={oDat} iAppMod={state.type} ChangeResponseInfo={(v: any) => changeODat(v)}/>,
},
{
label: 'test3',
key: '2',
visiable: true,
children: <BureauManageInfo oDat={oDat} iAppMod={state.type} ChangeBureauManageInfo={(v: any) => changeODat(v)}/>,
}
];
目前是用下面的方法。tabComList只存了key、children
setEditTabs((prev) => (prev.map(item => {
let tempVisible = item.visiable;
if (item.key == "4" || item.key == "5") {
tempVisible = false;
}
if (dataVForm.RadioSendKbn == dataVForm.radioRadioSendKbn && item.key == "6") {
// tab页隐藏
tempVisible = false;
}
let newChildren = tabComList.find(it => it.key == item.key)?.children;
return {...item, visiable: tempVisible, children: newChildren!};
}).filter(it => it.visiable == true)
));
回复
1个回答

test
2024-06-20
初始化和管理 state使用 useState 来管理 detailTabList,并使用 useEffect 在 oDat 更新时重新生成 detailTabList。
import React, { useState, useEffect } from 'react';
import { Tabs } from 'antd';
import RequestInfo from './RequestInfo';
import ResponseInfo from './ResponseInfo';
import BureauManageInfo from './BureauManageInfo';
const { TabPane } = Tabs;
const MyComponent = () => {
const [oDat, setODat] = useState(initialODat);
const [detailTabList, setDetailTabList] = useState([
{
label: 'test1',
key: '0',
visiable: true,
children: <RequestInfo oDat={oDat} iAppMod={state.type} ChangeRequestInfo={(v) => changeODat(v)} />,
},
{
label: 'test2',
key: '1',
visiable: true,
children: <ResponseInfo oDat={oDat} iAppMod={state.type} ChangeResponseInfo={(v) => changeODat(v)} />,
},
{
label: 'test3',
key: '2',
visiable: true,
children: <BureauManageInfo oDat={oDat} iAppMod={state.type} ChangeBureauManageInfo={(v) => changeODat(v)} />,
},
]);
// 更新 oDat 的方法
const changeODat = (newODat) => {
setODat(newODat);
};
// 根据 oDat 更新 detailTabList
useEffect(() => {
setDetailTabList([
{
label: 'test1',
key: '0',
visiable: true,
children: <RequestInfo oDat={oDat} iAppMod={state.type} ChangeRequestInfo={(v) => changeODat(v)} />,
},
{
label: 'test2',
key: '1',
visiable: true,
children: <ResponseInfo oDat={oDat} iAppMod={state.type} ChangeResponseInfo={(v) => changeODat(v)} />,
},
{
label: 'test3',
key: '2',
visiable: true,
children: <BureauManageInfo oDat={oDat} iAppMod={state.type} ChangeBureauManageInfo={(v) => changeODat(v)} />,
},
]);
}, [oDat, state.type]);
// 过滤和设置可见的 tab
const visibleTabs = detailTabList
.map((item) => {
let tempVisible = item.visiable;
if (item.key === '4' || item.key === '5') {
tempVisible = false;
}
if (dataVForm.RadioSendKbn === dataVForm.radioRadioSendKbn && item.key === '6') {
tempVisible = false;
}
return { ...item, visiable: tempVisible };
})
.filter((item) => item.visiable);
return (
<Tabs>
{visibleTabs.map((tab) => (
<Tabs.TabPane tab={tab.label} key={tab.key}>
{tab.children}
</Tabs.TabPane>
))}
</Tabs>
);
};
export default MyComponent;
- 初始化 State:
- 更新
oDat
方法: - 使用
useEffect
监听oDat
和state.type
的变化:每当oDat
或state.type
变化时,重新生成detailTabList
,确保子组件接收到最新的oDat - 过滤和设置可见的 Tabs:根据业务逻辑过滤掉不应该显示的 Tabs。只显示
visiable
为true
的 Tabs。 - 渲染 Tabs:使用、Tabs`组件渲染过滤后的 Tabs 列表。
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容