react如何给组件的props设置默认值?

如何给组件的props设置默认值?


interface CodeModCreatorProps {
    grpLvlColor: string[]
} 

function CodeModCreator({
    grpLvlColor: ['red', 'green']   // 这样传输会报错
}: CodeModCreatorProps) {
    
    return ( <>

    </> );
}

export default CodeModCreator;

QQ_1725198806049.png

阅读 909
1 个回答
✓ 已被采纳
interface CodeModCreatorProps {
  grpLvlColor: string[];
}

function CodeModCreator(
  { grpLvlColor }: CodeModCreatorProps = { grpLvlColor: ["red", "green"] }
) {
  return <></>;
}

export default CodeModCreator;
推荐问题
logo
Microsoft
子站问答
访问
宣传栏