Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transition when CSS change with State – React

I have a sidebar that minimize when i click on a button. I use a Class component and I have a state to deal with it.

If true, the sidebar width is 200px.
if false, it is 70px.

render() {
    const SidebarStyled = styled.div`
      width: ${this.state.sidebarOpen ? '200px' : '70px'};
      position: fixed;
      left: 0px;
      top: 0px;
      height: 100vh;
      background-color: #0c1635;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: center;

I would like to set a transition and I have no idea how.
The only solutions I found was with functional components, and my client want only class Components.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help ?

>Solution :

I guess, You are creating a const SidebarStyled and based on the state.sidebarOpen deciding the value of width, but the thing is the style is resolved and applied to the component and it does not change with the state change.
I would suggest you create two style const one with width: 200px and other with 70px and based on the state.sidebarOpen apply appropriate style const.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading