简体   繁体   English

重新渲染样式后的组件不应用过渡效果吗?

[英]Styled component does not apply transition effects when rerendered?

I'm using styled-components for having dynamic styling of my components but I can't seem to get it to work the way I want. 我正在使用styled-componentsstyled-components进行动态样式设置,但似乎无法使其按我想要的方式工作。

I'm trying to add a ease-in-out effect on buttons which are programmaticaly and dynamically focused. 我试图ease-in-out以编程方式和动态焦点的按钮上添加ease-in-out效果。

在此处输入图片说明

As you can see the styling is applied however the transition property does not take any effect. 如您所见,已应用样式,但是transition属性没有任何作用。 Is there a way to achieve this? 有没有办法做到这一点?

Just as an info note, the components (buttons) re-render every 3 seconds to toggle, might that be the cause? 就像信息说明一样,组件(按钮)每3秒重新渲染一次以进行切换,这可能是原因吗? If so how to work around this? 如果是这样,该如何解决?

Component 零件

class LanguageButton extends PureComponent {
    render() {
        const { children, theme, isFocussed, ...otherProps } = this.props;

        const Button = styled.div`
            border: 1px solid ${theme};
            margin-top: 10px;
            margin-bottom: 10px;
            background-color: transparent;
            text-align: center;
            padding-top: 25px;
            padding-bottom: 25px;
            min-height: 0;
            transition: all 0.1s ease-in-out;

            :hover {
                background-color: ${theme};
                transition: all 0.1s ease-in-out;
            }
            :active {
                background-color: ${theme};
            }

            &.isFocussed {
                transition: all 0.1s ease-in-out;
                background-color: #ebebeb;

                :hover {
                    background-color: ${theme};
                    transition: all 0.1s ease-in-out;
                }
                :active {
                    background-color: ${theme};
                }
            }
        `;

        return (
            <Button
                className={classnames("BUTTON BUTTON--50 GM--noselect", { isFocussed })}
                {...otherProps}
            >
                {children}
            </Button>
        );
    }
}

On first glance based on this code snippet, I'm assuming that you're adding / removing this "isFocused" class, and that is the root of your problem. 乍一看,基于此代码片段,我假设您正在添加/删除此“ isFocused”类,这就是问题的根源。 With transitions, you want to avoid adding / removing the class itself, because then the transition can't properly render because it's not there in the first place. 使用过渡时,您要避免添加/删除类本身,因为那样过渡就不能正确呈现,因为过渡过渡就没有了。

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

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