Reactjs styled components是在React组件(组件)周围使用wrapped styled()

Reactjs styled components是在React组件(组件)周围使用wrapped styled(),reactjs,styled-components,codesandbox,Reactjs,Styled Components,Codesandbox,我的应用程序在CodeSandbox中使用了样式化组件。请参考以下网址 每次我做一些更改时,控制台都会说 警告:道具'className'不匹配。 看起来您已经将styled()包装在您的React组件(component)上,但是没有将className prop传递给子级。除非在React组件中合成了className,否则不会呈现任何样式。 UI未按预期呈现。 有人知道我为什么会有这个问题吗?请查看上面的url 谢谢,链接实际上不起作用(或者我不明白您到底想显示什么),但是从错误消息来看

我的应用程序在CodeSandbox中使用了样式化组件。请参考以下网址

每次我做一些更改时,控制台都会说

警告:道具'className'不匹配。

看起来您已经将styled()包装在您的React组件(component)上,但是没有将className prop传递给子级。除非在React组件中合成了className,否则不会呈现任何样式。

UI未按预期呈现。 有人知道我为什么会有这个问题吗?请查看上面的url

谢谢,链接实际上不起作用(或者我不明白您到底想显示什么),但是从错误消息来看,您应该像这样传递className
styled()

基本上,您需要传递
this.props.className
props.className
或由
styled components
生成的解构
className
,并手动将其应用于要设置样式的组件。否则,您不会将
className
应用于任何内容,因此不会看到任何样式更改

工作示例:

components/LinkComponent.js(此
功能组件
接受由
styled()
道具
生成的
类名
,这些道具传递到下面创建的样式化组件——您需要手动将它们应用到
链接
组件)

components/Header.js(导入上面创建的样式化组件
StyledLink
,并利用它——传递给此组件的任何附加道具将自动传递给
函数
,但是,在这种情况下,您需要解构
道具
,以利用它)

从“React”导入React;
从“/StyledLink”导入StyledLink;
导出默认值()=>(
家
关于
文件夹
);

对于共享组件,最好使用forwardRef,或者您可以传递道具:

import React from 'react'
import styled from 'styled-components'

function MainComponent() {
  return (
    <LoadingStyled />
  )
})

const LoadingStyled = styled(LoadingComponent)`
  margin-top: 40px;
`
从“React”导入React
从“样式化组件”导入样式化
函数main component(){
返回(
)
})
const loadingstyle=styled(加载组件)`
边缘顶端:40px;
`

import React,{forwardRef}来自“React”
export const LoadingComponent=forwardRef((props,ref)=>{
返回(
我有所有的道具和风格,yeeeee!
)
})

没有forwardRef的备选方案

import React from 'react'

export const LoadingComponent = (props) => {
  return (
    <div {...props}>
      I got all props and styles, yeeeee!
    </div>
  )
}
从“React”导入React
导出常量加载组件=(道具)=>{
返回(
我有所有的道具和风格,yeeeee!
)
}

我遇到过类似的情况,我需要使用由样式化组件创建的组件,并将css属性传递给该组件。希望这有帮助

主要组件(在此处定义CSS属性)

从“组件/包装器”导入包装器
const CustomWrapper=styled(包装器)`
&:悬停{
背景色:蓝色;//定义要传递的css属性
}
`;
...
render(){
返回(
…//使用由“styled component”生成的CustomWrapper组件
)
}
`;
Wrapper.js-功能组件(此处使用定义的CSS)

const Wrapper=props=>{
const{className}=props;//看看下面如何解构和使用className
返回(
//这里使用“className”
{您的内容}
)
}

codesandbox无法显示必要的代码。请更新它。
import React from "react";
import StyledLink from "./StyledLink";

export default () => (
  <nav className="container">
    <StyledLink primary link="/">Home</StyledLink>
    <StyledLink danger link="/about">About</StyledLink>
    <StyledLink link="/portfolio">Portfolio</StyledLink>
  </nav>
);
import React from 'react'
import styled from 'styled-components'

function MainComponent() {
  return (
    <LoadingStyled />
  )
})

const LoadingStyled = styled(LoadingComponent)`
  margin-top: 40px;
`
import React, { forwardRef } from 'react'

export const LoadingComponent = forwardRef((props, ref) => {
  return (
    <div {...props}>
      I got all props and styles, yeeeee!
    </div>
  )
})
import React from 'react'

export const LoadingComponent = (props) => {
  return (
    <div {...props}>
      I got all props and styles, yeeeee!
    </div>
  )
}