Transition between two classes in React - React routing issue











up vote
0
down vote

favorite












I am trying to do a very simple React slide in submenu and I don't get why it doesn't work. I am toggling classes with setting the state.



<div id="sideNavBar" className={this.props.sideBar}>


Css:



#sideNavBar {
transform: translateX(-150px);
transition: transform 400ms ease-in;
background-image: linear-gradient(to right, #000000, #3B3B3B);
position: fixed;
z-index: 1;
min-height: 800px;
width: 180px;
opacity: 0.8;
}

#sideNavBar.SideBar {
transform: translateX(0);
}

#sideNavBar.SideBar_hidden {
transform: translateX(-100%);
}


---------------------------------------------------------ADDITIONAL COMPONENT
App.js
.....



 class App extends Component {
constructor() {
super();
this.state = {
isBlog: false,
isPhotography: false,
isDesign: false,
sideBar: 'SideBar_hidden'
}
this.selectPage = this.selectPage.bind(this);
}

selectPage = (event) => {
event.preventDefault();
if ((event.target.name)==='blog') {
this.setState({sideBar: 'SideBar', isBlog: true, isPhotography: false, isDesign: false})
}
else if((event.target.name)==='photography') {
this.setState({sideBar: 'SideBar', isBlog: false, isPhotography: true, isDesign: false})
}
else if ((event.target.name)==='design') {
this.setState({sideBar: 'SideBar', isPhotography: false, isDesign: true})
};
}

render() {
return (
<BrowserRouter>
<div className="App">
<Navigation selectPage={this.selectPage}/>
<div className="content">
<div className="body">
<Switch>
<Route exact path='/' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/></div>)}/>
</Switch>
<Switch>
<Route path='/blog' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Blog /></div>)}/>
</Switch>
<Switch>
<Route path='/photography' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Photography/></div>)}/>
</Switch>
<Switch>
<Route path='/design' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Design/></div>)}/>
</Switch>
</div>
<div className="footer">
</div>
</div>
</div>
</BrowserRouter>
);
}
}

export default App;


From "/" clicking any of the menu links (blog etc.) the sidebar menu just pops in. If I am at "/blog" route and refresh the page to set the state back to hidden and then click on the blog link it slides in. But if I am at /blog refresh the page to setthe state to hidden and click on not blog, instead of design it pops in again, instead of slides in. How can I work around this?










share|improve this question
























  • It seems to be working fine.
    – Tholle
    Nov 9 at 0:00










  • can you show us the component?
    – Sidonai
    Nov 9 at 0:01










  • Added some more code and new code. It looks like from /code clicking on code it slides in but if I am not at the matching URL it just pops in.
    – Gabriella Csernus
    Nov 9 at 1:14















up vote
0
down vote

favorite












I am trying to do a very simple React slide in submenu and I don't get why it doesn't work. I am toggling classes with setting the state.



<div id="sideNavBar" className={this.props.sideBar}>


Css:



#sideNavBar {
transform: translateX(-150px);
transition: transform 400ms ease-in;
background-image: linear-gradient(to right, #000000, #3B3B3B);
position: fixed;
z-index: 1;
min-height: 800px;
width: 180px;
opacity: 0.8;
}

#sideNavBar.SideBar {
transform: translateX(0);
}

#sideNavBar.SideBar_hidden {
transform: translateX(-100%);
}


---------------------------------------------------------ADDITIONAL COMPONENT
App.js
.....



 class App extends Component {
constructor() {
super();
this.state = {
isBlog: false,
isPhotography: false,
isDesign: false,
sideBar: 'SideBar_hidden'
}
this.selectPage = this.selectPage.bind(this);
}

selectPage = (event) => {
event.preventDefault();
if ((event.target.name)==='blog') {
this.setState({sideBar: 'SideBar', isBlog: true, isPhotography: false, isDesign: false})
}
else if((event.target.name)==='photography') {
this.setState({sideBar: 'SideBar', isBlog: false, isPhotography: true, isDesign: false})
}
else if ((event.target.name)==='design') {
this.setState({sideBar: 'SideBar', isPhotography: false, isDesign: true})
};
}

render() {
return (
<BrowserRouter>
<div className="App">
<Navigation selectPage={this.selectPage}/>
<div className="content">
<div className="body">
<Switch>
<Route exact path='/' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/></div>)}/>
</Switch>
<Switch>
<Route path='/blog' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Blog /></div>)}/>
</Switch>
<Switch>
<Route path='/photography' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Photography/></div>)}/>
</Switch>
<Switch>
<Route path='/design' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Design/></div>)}/>
</Switch>
</div>
<div className="footer">
</div>
</div>
</div>
</BrowserRouter>
);
}
}

export default App;


From "/" clicking any of the menu links (blog etc.) the sidebar menu just pops in. If I am at "/blog" route and refresh the page to set the state back to hidden and then click on the blog link it slides in. But if I am at /blog refresh the page to setthe state to hidden and click on not blog, instead of design it pops in again, instead of slides in. How can I work around this?










share|improve this question
























  • It seems to be working fine.
    – Tholle
    Nov 9 at 0:00










  • can you show us the component?
    – Sidonai
    Nov 9 at 0:01










  • Added some more code and new code. It looks like from /code clicking on code it slides in but if I am not at the matching URL it just pops in.
    – Gabriella Csernus
    Nov 9 at 1:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to do a very simple React slide in submenu and I don't get why it doesn't work. I am toggling classes with setting the state.



<div id="sideNavBar" className={this.props.sideBar}>


Css:



#sideNavBar {
transform: translateX(-150px);
transition: transform 400ms ease-in;
background-image: linear-gradient(to right, #000000, #3B3B3B);
position: fixed;
z-index: 1;
min-height: 800px;
width: 180px;
opacity: 0.8;
}

#sideNavBar.SideBar {
transform: translateX(0);
}

#sideNavBar.SideBar_hidden {
transform: translateX(-100%);
}


---------------------------------------------------------ADDITIONAL COMPONENT
App.js
.....



 class App extends Component {
constructor() {
super();
this.state = {
isBlog: false,
isPhotography: false,
isDesign: false,
sideBar: 'SideBar_hidden'
}
this.selectPage = this.selectPage.bind(this);
}

selectPage = (event) => {
event.preventDefault();
if ((event.target.name)==='blog') {
this.setState({sideBar: 'SideBar', isBlog: true, isPhotography: false, isDesign: false})
}
else if((event.target.name)==='photography') {
this.setState({sideBar: 'SideBar', isBlog: false, isPhotography: true, isDesign: false})
}
else if ((event.target.name)==='design') {
this.setState({sideBar: 'SideBar', isPhotography: false, isDesign: true})
};
}

render() {
return (
<BrowserRouter>
<div className="App">
<Navigation selectPage={this.selectPage}/>
<div className="content">
<div className="body">
<Switch>
<Route exact path='/' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/></div>)}/>
</Switch>
<Switch>
<Route path='/blog' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Blog /></div>)}/>
</Switch>
<Switch>
<Route path='/photography' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Photography/></div>)}/>
</Switch>
<Switch>
<Route path='/design' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Design/></div>)}/>
</Switch>
</div>
<div className="footer">
</div>
</div>
</div>
</BrowserRouter>
);
}
}

export default App;


From "/" clicking any of the menu links (blog etc.) the sidebar menu just pops in. If I am at "/blog" route and refresh the page to set the state back to hidden and then click on the blog link it slides in. But if I am at /blog refresh the page to setthe state to hidden and click on not blog, instead of design it pops in again, instead of slides in. How can I work around this?










share|improve this question















I am trying to do a very simple React slide in submenu and I don't get why it doesn't work. I am toggling classes with setting the state.



<div id="sideNavBar" className={this.props.sideBar}>


Css:



#sideNavBar {
transform: translateX(-150px);
transition: transform 400ms ease-in;
background-image: linear-gradient(to right, #000000, #3B3B3B);
position: fixed;
z-index: 1;
min-height: 800px;
width: 180px;
opacity: 0.8;
}

#sideNavBar.SideBar {
transform: translateX(0);
}

#sideNavBar.SideBar_hidden {
transform: translateX(-100%);
}


---------------------------------------------------------ADDITIONAL COMPONENT
App.js
.....



 class App extends Component {
constructor() {
super();
this.state = {
isBlog: false,
isPhotography: false,
isDesign: false,
sideBar: 'SideBar_hidden'
}
this.selectPage = this.selectPage.bind(this);
}

selectPage = (event) => {
event.preventDefault();
if ((event.target.name)==='blog') {
this.setState({sideBar: 'SideBar', isBlog: true, isPhotography: false, isDesign: false})
}
else if((event.target.name)==='photography') {
this.setState({sideBar: 'SideBar', isBlog: false, isPhotography: true, isDesign: false})
}
else if ((event.target.name)==='design') {
this.setState({sideBar: 'SideBar', isPhotography: false, isDesign: true})
};
}

render() {
return (
<BrowserRouter>
<div className="App">
<Navigation selectPage={this.selectPage}/>
<div className="content">
<div className="body">
<Switch>
<Route exact path='/' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/></div>)}/>
</Switch>
<Switch>
<Route path='/blog' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Blog /></div>)}/>
</Switch>
<Switch>
<Route path='/photography' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Photography/></div>)}/>
</Switch>
<Switch>
<Route path='/design' render={(routeProps) => (<div><SideBar sideBar={this.state.sideBar}/>
<Design/></div>)}/>
</Switch>
</div>
<div className="footer">
</div>
</div>
</div>
</BrowserRouter>
);
}
}

export default App;


From "/" clicking any of the menu links (blog etc.) the sidebar menu just pops in. If I am at "/blog" route and refresh the page to set the state back to hidden and then click on the blog link it slides in. But if I am at /blog refresh the page to setthe state to hidden and click on not blog, instead of design it pops in again, instead of slides in. How can I work around this?







html css reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 1:12

























asked Nov 8 at 23:55









Gabriella Csernus

4610




4610












  • It seems to be working fine.
    – Tholle
    Nov 9 at 0:00










  • can you show us the component?
    – Sidonai
    Nov 9 at 0:01










  • Added some more code and new code. It looks like from /code clicking on code it slides in but if I am not at the matching URL it just pops in.
    – Gabriella Csernus
    Nov 9 at 1:14


















  • It seems to be working fine.
    – Tholle
    Nov 9 at 0:00










  • can you show us the component?
    – Sidonai
    Nov 9 at 0:01










  • Added some more code and new code. It looks like from /code clicking on code it slides in but if I am not at the matching URL it just pops in.
    – Gabriella Csernus
    Nov 9 at 1:14
















It seems to be working fine.
– Tholle
Nov 9 at 0:00




It seems to be working fine.
– Tholle
Nov 9 at 0:00












can you show us the component?
– Sidonai
Nov 9 at 0:01




can you show us the component?
– Sidonai
Nov 9 at 0:01












Added some more code and new code. It looks like from /code clicking on code it slides in but if I am not at the matching URL it just pops in.
– Gabriella Csernus
Nov 9 at 1:14




Added some more code and new code. It looks like from /code clicking on code it slides in but if I am not at the matching URL it just pops in.
– Gabriella Csernus
Nov 9 at 1:14












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I have called SideBar component unnecessarily more than one time. Now it is only loaded once and animation works.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217895%2ftransition-between-two-classes-in-react-react-routing-issue%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    I have called SideBar component unnecessarily more than one time. Now it is only loaded once and animation works.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      I have called SideBar component unnecessarily more than one time. Now it is only loaded once and animation works.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        I have called SideBar component unnecessarily more than one time. Now it is only loaded once and animation works.






        share|improve this answer












        I have called SideBar component unnecessarily more than one time. Now it is only loaded once and animation works.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 12:29









        Gabriella Csernus

        4610




        4610






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217895%2ftransition-between-two-classes-in-react-react-routing-issue%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff