styled-components 是说将 styled() 包裹在你的 React 组件 (Component) 周围
styled-components is saying wrapped styled() around your React component (Component)
我的应用程序在 CodeSandbox 中使用样式化组件。请参考以下url
https://lrn6vmq297.sse.codesandbox.io/
每次我进行一些更改时,控制台都会显示。
Warning: Prop `className` did not match.
It looks like you've wrapped styled() around your React component (Component), but the className prop is not being passed down to a child. No styles will be rendered unless className is composed within your React component.
和 UI 未按预期呈现。
任何人都知道为什么我有这个问题?请看上面的url
谢谢
Link 并没有真正起作用(或者我不明白你到底想显示什么),但从错误消息看来你应该像这样传递 className
styled(<Component className={your source for classnames} />)
基本上,您需要传递 this.props.className
或 props.className
或由 styled-components
生成的解构 className
并手动将其应用于您要设置样式的组件.否则,您不会将 className
应用于任何内容,因此不会看到任何样式更改。
工作示例:
components/LinkComponent.js(这个functional component
接受由styled()
和props
生成的className
被传递到下面创建的样式化组件——您需要手动将它们应用到 Link
组件)
import React from "react";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
const LinkComponent = ({ className, children, link }) => (
<Link className={className} to={link}>
{children}
</Link>
);
LinkComponent.propTypes = {
className: PropTypes.string.isRequired,
link: PropTypes.string.isRequired,
children: PropTypes.string.isRequired
};
export default LinkComponent;
components/StyledLink.js(导入上面的 functional component
并将其传递给 styled()
-- 你也可以创建一个 styled themed 更新 styled()
个元素)
import styled from "styled-components";
import LinkComponent from "./LinkComponent";
const StyledLink = styled(LinkComponent)`
color: ${props => (!props.primary && !props.danger ? "#03a9f3" : "#ffffff")};
background-color: ${props => {
if (props.primary) return "#03a9f3";
if (props.danger) return "#f56342";
return "transparent";
}};
font-weight: bold;
margin-right: 20px;
padding: 8px 16px;
transition: all 0.2s ease-in-out;
border-radius: 4px;
border: 2px solid
${props => {
if (props.primary) return "#03a9f3";
if (props.danger) return "#f56342";
return "#03a9f3";
}};
&:hover {
color: ${props => (!props.primary && !props.danger ? "#0f7ae5" : "#ffffff")};
background-color: ${props => {
if (props.primary) return "#0f7ae5";
if (props.danger) return "#be391c";
return "transparent";
}};
text-decoration: none;
border: 2px solid ${props => (props.danger ? "#be391c" : "#0f7ae5")}};
}
`;
export default StyledLink;
components/Header.js(导入上面创建的样式化组件 StyledLink
并使用它——传递给该组件的任何附加属性将自动传递到 function
,但是,在这种情况下,您需要解构 prop
才能使用它)
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>
);
对于共享组件,最好使用 forwardRef,或者你可以只传递 props:
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>
)
})
没有 forwardRef 的替代方案。
import React from 'react'
export const LoadingComponent = (props) => {
return (
<div {...props}>
I got all props and styles, yeeeee!
</div>
)
}
我遇到过类似的情况,我需要使用由 styled-component 创建的组件,并将 css 属性 传递给该组件。希望这对您有所帮助!
主要成分(这里定义CSS属性)
import Wrapper from 'components/Wrapper'
const CustomWrapper = styled(Wrapper)`
&:hover {
background-color: blue; // defining css property I want to pass down
}
`;
...
render() {
return (
... <CustomWrapper /> // using my CustomWrapper component made from 'styled-component'
)
}
`;
Wrapper.js - 功能组件(这里使用定义的CSS)
const Wrapper = props => {
const { className } = props; // see how className is destructed and used below
return (
<div className={className}> // 'className' is used here
{YOUR_CONTENT}
</div>
)
}
我的应用程序在 CodeSandbox 中使用样式化组件。请参考以下url https://lrn6vmq297.sse.codesandbox.io/
每次我进行一些更改时,控制台都会显示。
Warning: Prop `className` did not match.
It looks like you've wrapped styled() around your React component (Component), but the className prop is not being passed down to a child. No styles will be rendered unless className is composed within your React component.
和 UI 未按预期呈现。 任何人都知道为什么我有这个问题?请看上面的url
谢谢
Link 并没有真正起作用(或者我不明白你到底想显示什么),但从错误消息看来你应该像这样传递 className
styled(<Component className={your source for classnames} />)
基本上,您需要传递 this.props.className
或 props.className
或由 styled-components
生成的解构 className
并手动将其应用于您要设置样式的组件.否则,您不会将 className
应用于任何内容,因此不会看到任何样式更改。
工作示例:
components/LinkComponent.js(这个functional component
接受由styled()
和props
生成的className
被传递到下面创建的样式化组件——您需要手动将它们应用到 Link
组件)
import React from "react";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
const LinkComponent = ({ className, children, link }) => (
<Link className={className} to={link}>
{children}
</Link>
);
LinkComponent.propTypes = {
className: PropTypes.string.isRequired,
link: PropTypes.string.isRequired,
children: PropTypes.string.isRequired
};
export default LinkComponent;
components/StyledLink.js(导入上面的 functional component
并将其传递给 styled()
-- 你也可以创建一个 styled themed 更新 styled()
个元素)
import styled from "styled-components";
import LinkComponent from "./LinkComponent";
const StyledLink = styled(LinkComponent)`
color: ${props => (!props.primary && !props.danger ? "#03a9f3" : "#ffffff")};
background-color: ${props => {
if (props.primary) return "#03a9f3";
if (props.danger) return "#f56342";
return "transparent";
}};
font-weight: bold;
margin-right: 20px;
padding: 8px 16px;
transition: all 0.2s ease-in-out;
border-radius: 4px;
border: 2px solid
${props => {
if (props.primary) return "#03a9f3";
if (props.danger) return "#f56342";
return "#03a9f3";
}};
&:hover {
color: ${props => (!props.primary && !props.danger ? "#0f7ae5" : "#ffffff")};
background-color: ${props => {
if (props.primary) return "#0f7ae5";
if (props.danger) return "#be391c";
return "transparent";
}};
text-decoration: none;
border: 2px solid ${props => (props.danger ? "#be391c" : "#0f7ae5")}};
}
`;
export default StyledLink;
components/Header.js(导入上面创建的样式化组件 StyledLink
并使用它——传递给该组件的任何附加属性将自动传递到 function
,但是,在这种情况下,您需要解构 prop
才能使用它)
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>
);
对于共享组件,最好使用 forwardRef,或者你可以只传递 props:
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>
)
})
没有 forwardRef 的替代方案。
import React from 'react'
export const LoadingComponent = (props) => {
return (
<div {...props}>
I got all props and styles, yeeeee!
</div>
)
}
我遇到过类似的情况,我需要使用由 styled-component 创建的组件,并将 css 属性 传递给该组件。希望这对您有所帮助!
主要成分(这里定义CSS属性)
import Wrapper from 'components/Wrapper'
const CustomWrapper = styled(Wrapper)`
&:hover {
background-color: blue; // defining css property I want to pass down
}
`;
...
render() {
return (
... <CustomWrapper /> // using my CustomWrapper component made from 'styled-component'
)
}
`;
Wrapper.js - 功能组件(这里使用定义的CSS)
const Wrapper = props => {
const { className } = props; // see how className is destructed and used below
return (
<div className={className}> // 'className' is used here
{YOUR_CONTENT}
</div>
)
}