【发布时间】:2019-04-12 00:11:10
【问题描述】:
如何让一个样式化的组件根据渲染它的 React 组件的状态来渲染不同的 css 规则?
以下不起作用:
class Container extends React.Component<ContainerProps, ContainerState> {
constructor(props: ContainerProps) {
super(props);
this.state = {
highlight: true,
dark: false
};
}
OuterWrapper = styled.div`
display: inline-block;
padding: 20px;
${this.state.dark && `
background-color: 'gray';
`};
`;
return (
<this.OuterWrapper>
...
</this.OuterWrapper>
);
}
TypeError:无法读取未定义的属性“dark” 在新容器中
【问题讨论】: