TypeScript:类型必须有一个“[Symbol.iterator]()”返回 iterator.ts(2488) 的方法
我查看了具有类似标题的其他问题,但无法将这些解决方案应用于此问题。 如果没有详细解释其他答案如何适用于此处,请不要将此问题标记为重复项。
目标是通过返回每个待生成 PDF 的对象数组来允许一次删除多个 PDF 文件- 作为数组中的对象删除。
PDF 文件列表通过 Props
(rowData) 作为字符串传入,并在 useEffect
中转换为对象。
interface TableRowData {
id: number;
name: string;
pdf: string; // "['bob.pdf', 'sam.pdf', 'art.pdf']"
}
interface ObjPdfsToRemove {
pdfs2EditType: string;
catSubcatId: number;
pdf2del: string;
}
interface DlgEditSubcatPdfsProps {
rowData: TableRowData;
onClose(): void;
pdfs2EditType: string;
}
export const DlgEditSubcatPDFs: FunctionalComponent<DlgEditSubcatPdfsProps> = ({ rowData, pdfs2EditType, onClose }) => {
const [currRowData, setCurrRowData] = useState<TableRowData>(rowData);
const [arrPdfNames, setArrPdfNames] = useState([]);
const [pdfs2Remove, setPdfs2Remove] = useState<ObjPdfsToRemove[] | undefined>(undefined);
useEffect(() => {
setCurrRowData(rowData);
setArrPdfNames(JSON.parse(rowData.pdf));
}, [rowData]);
PDF 在屏幕上显示如下:
<div class={style.fileList}>
{arrPdfNames !== undefined && arrPdfNames?.map((d) => {
return (
<div class={style.pdfsFlexRow}>
<Button className={style.pdfDelBtn} onClick={() => handleDelPdf(d)}>
DEL
</Button>
<div class={style.pdfFileName}>{d}</div>
</div>
);
})}
</div>
单击 Del 按钮时:
const handleDelPdf = (pdf2del: string) => {
setPdfs2Remove(pdfs2Remove !== undefined => [...pdfs2Remove, { //ADDED per VLAZ suggestion (pretty sure my syntax incorrect)
pdfs2EditType: pdfs2EditType,
catSubcatId: rowData.id,
pdf2del: pdf2del,
}]);
};
错误位于 handleDelPdf
(紧邻上方)的这一行:setPdfs2Remove(pdfs2Remove => [...pdfs2Remove, {
^^^^^^^^^^^^^^^
当鼠标悬停在 ...pdfs2Remove
时,TS 错误显示:
Type 'ObjPdfsToRemove[] | undefined' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
-----
(parameter) pdfs2Remove: ObjPdfsToRemove[] | undefined
如何修复此错误?
更新:
- 在
handleDelPdf
函数中添加了IF条件if (pdfs2Remove !== undefined)
,但错误仍然相同。
最初添加了这样的条件测试:
if setPdfs2Remove(pdfs2Remove !== undefined){ //--------------------- ADDED (same error)
setPdfs2Remove(pdfs2Remove => [...pdfs2Remove, {
pdfs2EditType: pdfs2EditType,
catSubcatId: rowData.id,
pdf2del: pdf2del,
}]);
}; //--------------------------------------------------------------- ADDED (same error)
- 删除了上面的 (1) 并更改了
setPdfs2Remove()
函数本身,如下所示(这就是代码现在在原始问题中显示的方式):
setPdfs2Remove(pdfs2Remove !== undefined => {
但这也不对。
I reviewed the other questions with a similar title, but cannot apply those solutions to this question. Please do not mark this question a duplicate without explaining in detail how the other answer applies here.
Goal is to allow deletion of several PDF files at once by returning an array of objects with each PDF-to-be-deleted as an object in the array.
The list of PDF files is passed in as a string via the Props
(rowData) and converted to an object in useEffect
.
interface TableRowData {
id: number;
name: string;
pdf: string; // "['bob.pdf', 'sam.pdf', 'art.pdf']"
}
interface ObjPdfsToRemove {
pdfs2EditType: string;
catSubcatId: number;
pdf2del: string;
}
interface DlgEditSubcatPdfsProps {
rowData: TableRowData;
onClose(): void;
pdfs2EditType: string;
}
export const DlgEditSubcatPDFs: FunctionalComponent<DlgEditSubcatPdfsProps> = ({ rowData, pdfs2EditType, onClose }) => {
const [currRowData, setCurrRowData] = useState<TableRowData>(rowData);
const [arrPdfNames, setArrPdfNames] = useState([]);
const [pdfs2Remove, setPdfs2Remove] = useState<ObjPdfsToRemove[] | undefined>(undefined);
useEffect(() => {
setCurrRowData(rowData);
setArrPdfNames(JSON.parse(rowData.pdf));
}, [rowData]);
The PDFs are displayed on screen like this:
<div class={style.fileList}>
{arrPdfNames !== undefined && arrPdfNames?.map((d) => {
return (
<div class={style.pdfsFlexRow}>
<Button className={style.pdfDelBtn} onClick={() => handleDelPdf(d)}>
DEL
</Button>
<div class={style.pdfFileName}>{d}</div>
</div>
);
})}
</div>
When the Del button is clicked:
const handleDelPdf = (pdf2del: string) => {
setPdfs2Remove(pdfs2Remove !== undefined => [...pdfs2Remove, { //ADDED per VLAZ suggestion (pretty sure my syntax incorrect)
pdfs2EditType: pdfs2EditType,
catSubcatId: rowData.id,
pdf2del: pdf2del,
}]);
};
THE ERROR is in handleDelPdf
(immediately above), on this line:setPdfs2Remove(pdfs2Remove => [...pdfs2Remove, {
^^^^^^^^^^^^^^
When hover the ...pdfs2Remove
, the TS Error reads:
Type 'ObjPdfsToRemove[] | undefined' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
-----
(parameter) pdfs2Remove: ObjPdfsToRemove[] | undefined
How to fix this error?
Updates:
- Added IF condition
if (pdfs2Remove !== undefined)
to thehandleDelPdf
function, but error remained the same.
Initially added the conditional test like this:
if setPdfs2Remove(pdfs2Remove !== undefined){ //--------------------- ADDED (same error)
setPdfs2Remove(pdfs2Remove => [...pdfs2Remove, {
pdfs2EditType: pdfs2EditType,
catSubcatId: rowData.id,
pdf2del: pdf2del,
}]);
}; //--------------------------------------------------------------- ADDED (same error)
- Removed (1) above and changed the
setPdfs2Remove()
func itself, like this (this is how the code is now displayed in the original question):
setPdfs2Remove(pdfs2Remove !== undefined => {
but that's not right either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论