简体   繁体   English

如何转换mui背景颜色字符串?

[英]How to transition mui background color string?

I have the following component where I want to change the color depending on user type, issue is when I use gradient... the color get stops from transitioning, if I use simple colors like bule, red, green.... then it works but at the current state in code the colors changes but whiteout the slowed transition... how to solve this?我有以下组件,我想根据用户类型更改颜色,问题是当我使用渐变时...颜色停止过渡,如果我使用简单的 colors,如蓝色、红色、绿色....然后它有效,但在代码中的当前 state 中,colors 发生了变化,但掩盖了缓慢的过渡……如何解决这个问题?

 const Home: React.FC = () => { const _authContext = useContext(authContext); const hexUserArr = ['linear-gradient(360deg, #fe6b8b 30%, #7DF9FF 70%)']; const hexAdminArr = ['linear-gradient(360deg, #dfe566 30%, #7DF334 70%)']; return ( <div style={{ minHeight: '100vh', marginTop: 0, marginBottom: -50, justifyContent: 'center', background: _authContext.starterUserType === 'admin'? hexAdminArr[0]: hexUserArr[0], transition: 'background 10s ease', }} > </div> ); }; export default Home;

It's how React works, you should rewrite following lines这就是 React 的工作方式,你应该重写以下几行

const hexUserArr = ['linear-gradient(360deg, #fe6b8b 30%, #7DF9FF 70%)'];
const hexAdminArr = ['linear-gradient(360deg, #dfe566 30%, #7DF334 70%)'];

with React.useState hook使用React.useState 钩子

const [hexUserArr, setHexUserArr] = React.useState([...])

Or if first answer didn't work, look here, that is how I dealt mine:或者,如果第一个答案不起作用,请看这里,这就是我处理我的方式:

const getStyle = () => {
        if(user !== null)
        {
            const deg = -135 + Math.round(360 * user.vacationsNum / defaultNumVacations / 15) * 15;

            return { 
                    ROTATE: deg
            }
        }
    }

const PROGRESS_LAYER = user ? {
        width: "5rem",
        height: "5rem",
        transform: `rotate(${getStyle().ROTATE}deg)`
    } : "";

return (
     <div style={PROGRESS_LAYER} />
)

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

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