javascript - styled-component .attrs - React 无法识别 prop

标签 javascript reactjs styled-components

我正在尝试将 Prop 传递到我的样式化组件中。它按预期工作,但 React 抛出已知的“Unknown Prop”错误。

我尝试在许多地方使用传播运算符,但都没有用。

我想将 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)

最佳答案

更新:使用 transient 属性

随着release 5.1.0你可以使用 transient props。这样你就不需要额外的包装器,即减少了不必要的代码:

Transient props are a new pattern to pass props that are explicitly consumed only by styled components and are not meant to be passed down to deeper component layers. Here's how you use them:

const Comp = styled.div`
  color: ${props => props.$fg || 'black'};
`;

render(<Comp $fg="red">I'm red!</Comp>);

注意 prop 上的美元符号 ($) 前缀;这将它标记为 transient ,并且样式化组件知道不要将它添加到呈现的 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
  /* ...  */
`;

关于javascript - styled-component .attrs - React 无法识别 prop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57586654/

相关文章:

javascript - 如何在图像上绘制 Angular2 Ionic2

javascript - ngClass 什么时候评估函数/如何在自定义指令中模仿它

javascript - 多个错误 auth0 react apollo babel webpack

javascript - 如何使用样式组件禁用 WebStorm 的这种行为

javascript - 样式...的组件是动态创建的。您可能会看到此警告,因为您在另一个组件中调用了 styled

javascript - 使用 Angular 从 Firestore 数据库返回多个集合

javascript - 如何在 JavaScript 中刷新屏幕数据而不会使页面变慢?

javascript - 不确定如何从子组件更新父状态

javascript - 编译失败。 ReactJs

reactjs - 如何在样式组件中定义 className