这是我的代码片段
{animals.map((animal: Array<any>, i: number) => {
return <span id={i} className={index === i ? "animal active" : "animal"} onClick={handleAnimalSelect}></span>;
})}
错误
(JSX attribute) React.HTMLAttributes<HTMLSpanElement>.id?: string
Type 'number' is not assignable to type 'string'.ts(2322)
index.d.ts(1760, 9): The expected type comes from property 'id' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>'
答案 0 :(得分:3)
您已将 i
声明为 number
,但 id
必须为 string
。将 id={i}
更改为 id={i.toString()}
,将 key={i}
更改为 key={i.toString()}
。