样式组件不使用道具
Styled-component not taking props
我正在尝试将道具传递给我的样式化组件。我正在使用 styled-component npm 依赖项。
import React, { Component } from 'react';
import styled from 'styled-components';
class WidgetContainer extends Component {
render() {
return (
<Wrapper name='widget-container'>
</Wrapper>
);
}
}
const mapStateToProps = state => {
return {
user: state.user,
bannerStatus: true
};
};
export default withRouter(connect(mapStateToProps)(WidgetContainer));
const Wrapper = styled.div`
display: flex;
flex-direction: column;
width: 100%;
min-height: 100vh;
margin: auto;
align-items: center;
justify-content: flex-start;
background: -webkit-linear-gradient(top, rgba(162, 209, 234, 1) 0%, rgba(56, 83, 132, 1) 100%);
padding-top: ${(props) => {
console.log(props.bannerStatus)
console.log('DATA')
return props.user ? '160px' : '126px'}};
overflow-y: auto;
padding-bottom: 40px;
@media(max-width: 768px) {
padding-top: 126px;
padding-bottom: 5px;
}
`;
console.log你可以看到returnsundefined。即使该值是硬编码的。三元运算符因此不起作用。
这是文档:https://www.styled-components.com/docs/basics#adapting-based-on-props。
您仅在 WidgetContainer
中将状态映射到道具。您的 Wrapper
组件未传递任何道具。
尝试
<Wrapper bannerStatus={props.bannerStatus} name='widget-container' />
或
<Wrapper user={props.user} bannerStatus={props.bannerStatus} name='widget-container' />
我正在尝试将道具传递给我的样式化组件。我正在使用 styled-component npm 依赖项。
import React, { Component } from 'react';
import styled from 'styled-components';
class WidgetContainer extends Component {
render() {
return (
<Wrapper name='widget-container'>
</Wrapper>
);
}
}
const mapStateToProps = state => {
return {
user: state.user,
bannerStatus: true
};
};
export default withRouter(connect(mapStateToProps)(WidgetContainer));
const Wrapper = styled.div`
display: flex;
flex-direction: column;
width: 100%;
min-height: 100vh;
margin: auto;
align-items: center;
justify-content: flex-start;
background: -webkit-linear-gradient(top, rgba(162, 209, 234, 1) 0%, rgba(56, 83, 132, 1) 100%);
padding-top: ${(props) => {
console.log(props.bannerStatus)
console.log('DATA')
return props.user ? '160px' : '126px'}};
overflow-y: auto;
padding-bottom: 40px;
@media(max-width: 768px) {
padding-top: 126px;
padding-bottom: 5px;
}
`;
console.log你可以看到returnsundefined。即使该值是硬编码的。三元运算符因此不起作用。
这是文档:https://www.styled-components.com/docs/basics#adapting-based-on-props。
您仅在 WidgetContainer
中将状态映射到道具。您的 Wrapper
组件未传递任何道具。
尝试
<Wrapper bannerStatus={props.bannerStatus} name='widget-container' />
或
<Wrapper user={props.user} bannerStatus={props.bannerStatus} name='widget-container' />