我正在尝试将一个道具传递到我的样式组件中。它可以按预期工作,但是React会抛出已知的“未知属性”错误。
我试图在许多地方使用散布运算符,但都没有起作用。
我想将prop传递给以下样式元素:
const StyledBackgroundImage = styled(BackgroundImage).attrs(({minHeight}) => ({
minHeight: minHeight || "60vh",
}))`
min-height: ${({minHeight}) => minHeight};
/* ... */
`;
父组件:
const ImageWithText = ({imageData, minHeight, children}) => {
return (
<StyledBackgroundImage
Tag="div"
backgroundColor={'#000000'}
fluid={imageData}
minHeight={minHeight}
>
{children}
</StyledBackgroundImage>
)
}
以及如何在页面上使用它:
<ImageWithText imageData={data.headerBackgroundImage.childImageSharp.fluid} minHeight='50vh'>
我希望它能正常工作,但不会出现以下错误:
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 lowercase `minheight` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
in div (created by BackgroundImage)
in BackgroundImage (created by Context.Consumer)
in StyledComponent (created by ImageWithText__StyledBackgroundImage)
in ImageWithText__StyledBackgroundImage (at ImageWithText.js:32)
in ImageWithText (at pages/index.js:20)
in section (created by Context.Consumer)
in StyledComponent (created by LayoutComponents__Section)
in LayoutComponents__Section (at pages/index.js:19)
in main (at layout.js:10)
in Layout (at pages/index.js:17)
in IndexPage (created by HotExportedIndexPage)
in AppContainer (created by HotExportedIndexPage)
in HotExportedIndexPage (created by PageRenderer)
in PageRenderer (at json-store.js:93)
in JSONStore (at root.js:51)
in RouteHandler (at root.js:73)
in div (created by FocusHandlerImpl)
in FocusHandlerImpl (created by Context.Consumer)
in FocusHandler (created by RouterImpl)
in RouterImpl (created by Context.Consumer)
in Location (created by Context.Consumer)
in Router (created by EnsureResources)
in ScrollContext (at root.js:64)
in RouteUpdates (at root.js:63)
in EnsureResources (at root.js:61)
in LocationHandler (at root.js:119)
in LocationProvider (created by Context.Consumer)
in Location (at root.js:118)
in Root (at root.js:127)
in _default (at app.js:65)
答案 0 :(得分:12)
通过release 5.1.0,您可以使用transient props
。这样,您不需要额外的包装程序,即减少了不必要的代码:
临时道具是一种传递道具的新模式,道具仅由样式化组件显式使用,而无意传递给更深的组件层。使用方法如下:
const Comp = styled.div`
color: ${props => props.$fg || 'black'};
`;
render(<Comp $fg="red">I'm red!</Comp>);
请注意道具上的美元符号($)前缀;这标记为暂时的,样式化组件不知道将其添加到呈现的DOM元素中,也不将其进一步传递到组件层次结构中。
组件:
<ImageWithText
$imageData={data.headerBackgroundImage.childImageSharp.fluid} // notice the '$'
minHeight='50vh'>
样式化组件的声明:
const StyledBackgroundImage = styled(BackgroundImage).attrs(({$minHeight}) => ({
minHeight: minHeight || "60vh",
}))`
min-height: ${({$minHeight}) => $minHeight}; // notice the '$' before the prop name
/* ... */
`;
答案 1 :(得分:2)
为防止组件将所有属性传递给DOM元素,请创建组件的包装,但不要通过“对象分解”将自定义属性传递给子组件。这样,您可以为包装的组件设置样式,styled-components
可以访问道具,但是子代将没有自定义属性,并且警告将消失。
import React from 'react';
import styled from 'styled-components';
import Typography from '@material-ui/core/Typography'
const WrappedTypography = ({ maxWidth, ...props }) => {
return <Typography {...props} />
}
const Text = styled(WrappedTypography) `
${({ maxWidth }) => maxWidth ? `max-width: ${maxWidth}` : null}
`
export default Text;
您可以在Mozilla文档中了解有关解构的更多信息。
答案 2 :(得分:0)
正确的语法应为:
uint
编辑: 实际上,如果您只想使用默认样式,为什么不直接在默认样式中使用它:
const StyledBackgroundImage = styled(BackgroundImage).attrs({
minHeight: props => props.minHeight || "60vh"
})`
min-height: ${({ minHeight }) => minHeight};
/* ... */
`;
`