样式的组件过渡动画不起作用
如何使用过渡进行按钮更改样式?现在,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论