简体   繁体   English

通过使用样式组件单击按钮来切换样式

[英]Toggle style by clicking on button using styled-component

After onClick , it starts function onToggleLiked in app.js which toggle like property and return(or not) like to item.js .onClick之后,它在app.js中启动 function onToggleLiked ,它属性一样切换并返回(或不)item.js After that, AppListItem check if like has appeared, and use styles from const theme if the result is true , else - from defaultProps .之后, AppListItem检查是否出现了like ,如果结果为true ,则使用const 主题中的 styles ,否则 - 来自defaultProps

How to do that?怎么做? I've tried to create a function, but failed.我试图创建一个 function,但失败了。

let AppListItem = styled.div`
.fa-heart {
          transform: ${props => props.theme.ts};
          opacity: ${props => props.theme.op};
      }`

AppListItem.defaultProps = {
   theme: {
    op: "0",
    ts: "translateX(30px)"
}}

const theme = {
    op: "1",
    ts: "translateX(0px)"
}
export default class PostListItem extends Component {
    render() {
           const {like} = this.props;
           if (like) {?}

return (
        <AppListItem>
            <ItemLabel as="span"
            onClick={onToggleLiked}>
                {label}
            </ItemLabel>

You can simply spread the desired properties, depending on the liked condition.您可以根据喜欢的条件简单地传播所需的属性。

render() {
   const { like } = this.props;
   const props = like ? { theme: theme } : AppListItem.defaultProps;
   return <AppListitem {...props}>....;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM