样式的组件过渡动画不起作用

发布于 2025-02-13 07:20:25 字数 1255 浏览 0 评论 0原文

如何使用过渡进行按钮更改样式?现在,Reactjs重新启动组件,并且没有显示任何动画按钮,

<S.Button onClick={() => setActive(!isActive)} isActive={isActive}>
            {/* {isActive ? 'Daily stats' : 'All time stats'} */}
            asd
        </S.Button>
export const Button = styled.button<{ isActive: boolean }>`
    font-family: ${({ theme }) => theme.font.family.book};
    background-color: ${({ isActive, theme }) =>
        isActive ? '#cae8ee' : theme.color.white};
    border: 1px solid
        ${({ isActive, theme }) => (isActive ? theme.color.primary : '#f0f0f0')};
    transition: all 0.3s ease-in-out;

    &::before {
        content: '';
        background-color: ${({ isActive, theme }) =>
            isActive ? theme.color.primary : '#f0f0f0'};
        left: ${({ isActive }) => (isActive ? '150px' : '2px')};
        transition: all 0.3s ease-in-out;
    }
`;

我发现该问题是在按钮上获取数据的使用效率挂钩。因此,它在不显示任何动画的情况下重新注册该按钮。

    useEffect(() => {
        if (statsButtonActive) {
            fetchDailyData();
        } else {
            fetchAllTimeData();
        }
        // eslint-disable-next-line
    }, [statsButtonActive]);

是否有任何解决方案可以停止恢复按钮?

How do I make the button change styling using transition? Right now Reactjs rerenders the component and does not show any animations for the button

<S.Button onClick={() => setActive(!isActive)} isActive={isActive}>
            {/* {isActive ? 'Daily stats' : 'All time stats'} */}
            asd
        </S.Button>
export const Button = styled.button<{ isActive: boolean }>`
    font-family: ${({ theme }) => theme.font.family.book};
    background-color: ${({ isActive, theme }) =>
        isActive ? '#cae8ee' : theme.color.white};
    border: 1px solid
        ${({ isActive, theme }) => (isActive ? theme.color.primary : '#f0f0f0')};
    transition: all 0.3s ease-in-out;

    &::before {
        content: '';
        background-color: ${({ isActive, theme }) =>
            isActive ? theme.color.primary : '#f0f0f0'};
        left: ${({ isActive }) => (isActive ? '150px' : '2px')};
        transition: all 0.3s ease-in-out;
    }
`;

I found the issue to be the useEffect hook that fetches data on button press. Therefore, it rerenders the button without showing any animations.

    useEffect(() => {
        if (statsButtonActive) {
            fetchDailyData();
        } else {
            fetchAllTimeData();
        }
        // eslint-disable-next-line
    }, [statsButtonActive]);

Are there any solutions to stop rerendering the button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文