点击不同标签弹出相同模态框,如何按标签类型触发对应对话框?
问题原因
你的代码中,handleModalExport函数的条件判断逻辑存在错误。ExportTypes.some(e => e.id === 'AllRecommendations')是在检查数组中是否存在该ID,而这个判断始终为true(因为数组里确实包含这个元素),所以无论点击哪个菜单选项,都会执行第一个if分支的代码,弹出「All Recommendations」对应的模态框。
修复方案
需要把点击的菜单选项ID传递给处理函数,再根据这个ID判断应该弹出哪个模态框:
const ExportTypes: Array<{ label: string; id: string }> = [ { label: 'All Recommendations', id: 'AllRecommendations' }, { label: 'Entities By type', id: 'EntitiesBytype' }, ]; const handleModalExport = useCallback( (selectedId: string) => { if (selectedId === 'AllRecommendations') { openModalDialog({ title: t('Export all recommendations'), renderContent: () => <RecommendationExportModel />, }); } else if(selectedId === 'EntitiesBytype') { openModalDialog({ title: t('Export all'), renderContent: () => <RecommendationExportEntitiesModel />, }); } }, [openModalDialog, t], ); {ExportTypes.map((objType) => ( <MenuItem key={objType.id} onClick={() => handleModalExport(objType.id)}> {t(objType.label)} </MenuItem> ))}
关键修改点
- 让
handleModalExport接收selectedId参数,代表当前点击的菜单ID - 将条件判断从检查数组是否包含该ID,改为直接对比传入的
selectedId - 在
MenuItem的onClick中,通过箭头函数把当前objType.id传递给handleModalExport
内容的提问来源于stack exchange,提问作者simran
相关产品推荐
相关产品推荐

