React-router: componentWillUnMount is not called when the router changes

Created on 6 Aug 2015  路  10Comments  路  Source: ReactTraining/react-router

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);

Most helpful comment

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

All 10 comments

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

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

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

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.

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?

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

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

Ahh thank you @ignu !

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArthurRougier picture ArthurRougier  路  3Comments

stnwk picture stnwk  路  3Comments

ackvf picture ackvf  路  3Comments

Waquo picture Waquo  路  3Comments

tomatau picture tomatau  路  3Comments