【发布时间】:2021-06-10 20:28:20
【问题描述】:
我对 TS 相当陌生,并试图了解如何将可选道具传递给这个对我来说有点复杂的组件。我以为你可以使用 ?对于您希望成为可选的道具,但我收到以下错误:
绑定模式参数在实现签名中不能是可选的。
我如何将可选的 props 传递给这个组件?
带有可选道具
export const BGVideo = React.memo(function BGVideo({ src, children, bgColor? }: any) {
return ( ^^^^^^^^
<BackgroundVideoContainer>...some other stuff...
)
});
原创
export const BGVideo = React.memo(function BGVideo({ src, children }: any) {
return (
<BackgroundVideoContainer>...some other stuff...
)
});
【问题讨论】:
-
您可以在类型定义中使用
?,但这是冒号的右侧...
标签: reactjs typescript react-typescript