react-router icon indicating copy to clipboard operation
react-router copied to clipboard

componentWillUnMount is not called when the router changes

Open hon opened this issue 11 years ago • 10 comments

I am using flux in my project. I listen and register action in 'componentDidMount' method:

  componentDidMount(){
    //listen
    AddressStore.addInitListener(this._onInit.bind(this));
    AddressStore.addLoadListener(this._onLoad.bind(this));
    AddressStore.addGetListener(this._onGet.bind(this));

    //trigger action in ajax
    Services.Address.fetchInfo({
      init: AddressActions.init,
      load: AddressActions.load
    });
  }

and then remove listener in 'componentWillUnMount':

componentWillUnMount(){
    console.log('unmount'); 
    AddressStore.removeInitListener(this._onInit.bind(this));
    AddressStore.removeLoadListener(this._onLoad.bind(this));
    AddressStore.removeGetListener(this._onGet.bind(this));
  }

But if the url is changed, componentWillUnMount is not called. Below are the router configrations:

let history = new HashHistory

let router = (
  <Router history={ history }>
    <Route path="/insurance" component={Pages.InsurancePage} />
    <Route path="/cosmetology" component={Pages.CosmetologyPage} />
    <Route path="/service-list" component={Pages.ServiceListPage} />
    <Route path="/pay" component={Pages.PayPage} />
    <Route path="/order-status" component={Pages.OrderStatusPage} />
    <Route path="/address-list" component={Pages.AddressListPage} />
    <Route path="/maintenance" component={Pages.MaintenancePage}>
      <Route path="car-exchange" component={Pages.CarExchangePage} />
    </Route>
    <Route path="/orders" component={Pages.OrdersPage}>
      <Route path="order-details" component={Pages.OrderDetailsPage} >
        <Route path="order-evaluation" component={Pages.orderevaluationPage} />
      </Route>
    </Route>
    <Route path="/packages" component={Pages.PackagesPage} />
    <Route path="/register" component={Pages.RegisterPage}>
      <Route path="register-done" component={Pages.RegisterDonePage} />
    </Route>
    <Route path="/sign" component={Pages.SignPage} />
    <Route path="/single-wash" component={Pages.SingleWashPage} />
    <Route path="/user-center" component={Pages.UserCenterPage}>
      <Route path="car-exchange" component={Pages.CarExchangePage} />
      <Route path="renewal-fee" component={Pages.RenewalFeePage} />
    </Route>
  </Router>
);

React.render(router, document.body);

hon avatar Aug 06 '15 10:08 hon

@hon Did you resolve this problem? I'm having this happen also, without using Flux.

mcavaliere avatar Sep 16 '15 19:09 mcavaliere

I'm pretty sure I'm seeing this too, any solutions would be appreciated...

chrismcv avatar Sep 17 '15 10:09 chrismcv

the method is componentWillUnmount without capital M, that might be the problem

knowbody avatar Sep 17 '15 13:09 knowbody

Sorry, in my case it was spelled correctly. But I resolved the problem, there was a React.render being used inside of a componentDidMount, which is not a good thing. Removing that fixed the issue, and componentWillUnmount fires now.

mcavaliere avatar Sep 17 '15 14:09 mcavaliere

I am not seeing react-router unmount component when the route is a sub-route:

<Router history={history}>
    <Route path="path/:paramId" component={ComponentForRoute} />
</Router>

So even if I switch between path/1 and path/2, componentWillUnmount never gets called for the instance of ComponentForRoute. Any suggestions? Is this how react-router is suppose to work? If so, any suggestions on handling route changes?

alexprice1 avatar Feb 11 '16 21:02 alexprice1

Agreed, would love an answer to this! :+1:

geraldarthurdavis avatar Feb 18 '16 00:02 geraldarthurdavis

should be taken to Stack Overflow, but @alexprice91 @garrettmaring https://github.com/reactjs/react-router/blob/master/docs/guides/ComponentLifecycle.md

ignu avatar Feb 18 '16 00:02 ignu

Ahh thank you @ignu !

geraldarthurdavis avatar Feb 18 '16 00:02 geraldarthurdavis

Updated link for those of us still struggling with 2.x/3.x: https://github.com/ReactTraining/react-router/blob/800991cd2044293718fccd3763b9d91c2f811296/docs/guides/ComponentLifecycle.md

tobyjsullivan avatar May 17 '17 23:05 tobyjsullivan

i have almost the same trouble with React , the problem is that the top element is App and never get chance to leave or unmount, is there any builtin function that i can call to when I leave the app to another route? thank you for help

oladhari avatar Jul 18 '18 08:07 oladhari