Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide component instead of unmounting it?

I have my routes defined like this:

  <Route component={App}>
    <IndexRoute component={Main}/>
    <Route path="/foo" component={Foo}/>
    <Route path="/bar" component={Bar}/>
  </Route>

By default component gets unmounted when you transition from Foo to Bar. Since I have a computation-heavy code in the component Foo (Google Maps with custom animations) I would like to prevent unmount and hide this component instead, so when user goes back it will load instantly.

How to achieve this?

like image 796
Kertis van Kertis Avatar asked Oct 27 '16 00:10

Kertis van Kertis


Video Answer


1 Answers

In order to achieve your requirement, you can not use two separate routes for Foo and Bar. You can set only one route for a container component(We name it FooBar here) . The FooBar combines Foo and Bar. Use the state of FooBar to change the visibility of the two components in the FooBar's render function .

like image 103
Jason Xu Avatar answered Oct 07 '22 01:10

Jason Xu