我正在尝试将道具传递到我的样式组件中。它按预期工作,但 React 抛出了已知的“Unknown Prop”错误。
我试图在很多地方使用传播运算符,但都没有奏效。
我想将道具传递给的样式组件:
const StyledBackgroundImage = styled(BackgroundImage).attrs(({minHeight}) => ({
minHeight: minHeight || "60vh",
}))`
min-height: ${({minHeight}) => minHeight};
/* ... */
`;
Run Code Online (Sandbox Code Playgroud)
父组件:
const ImageWithText = ({imageData, minHeight, children}) => {
return (
<StyledBackgroundImage
Tag="div"
backgroundColor={'#000000'}
fluid={imageData}
minHeight={minHeight}
>
{children}
</StyledBackgroundImage>
)
}
Run Code Online (Sandbox Code Playgroud)
以及我如何在页面上使用它:
<ImageWithText imageData={data.headerBackgroundImage.childImageSharp.fluid} minHeight='50vh'>
Run Code Online (Sandbox Code Playgroud)
我希望它可以工作,确实可以,但并非没有以下错误:
Warning: React does not recognize the `minHeight` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as …Run Code Online (Sandbox Code Playgroud)