发生一些错误 地图不起作用



>字符串数组有映射函数吗? 字符串数组似乎没有映射和循环函数或其他东西。 我不知道解决这个问题。

List:string[] = ["a","b","c"]
<CheckList {...props.List} />

const CheckList = (checks: string[]) => {
return (
<React.Fragment>
{checks.map(item => {
return <p>{item}</p>;
})}
</React.Fragment>
);
};

Sohail 的答案是正确的,因为你没有正确传入它,但它需要作为 props 对象的一部分传入。

因为它是一个 react 组件,它仍然需要接受 props,它不能将字符串数组作为参数。

interface CheckListProps {
checks: string[];
}
const CheckList = ({ checks }: CheckListProps) => {
// ...

在代码中,您将列表项作为单个道具进行传播。您必须将列表作为数组传递。

试试这个。


List:string[] = ["a","b","c"]
<CheckList checks={props.List} />

const CheckList = ({ checks }: {checks: string[]}) => {
return (
<React.Fragment>
{checks.map(item => {
return <p>{item}</p>;
})}
</React.Fragment>
);
};

最新更新