2017-03-01 39 views
0

我使用的反应 - 路由器的路由结构,像这样:反应路由器:调用componentWillUnmount在不同的非嵌套路线

const routes = (
    <Router history={browserHistory}> 
    <Route path="/main" component={MainPage}></Route> 
    <Route path="/charts/:chartID" component={App}></Route> 
    </Router> 
) 

我试图做一个呼叫流星方法,当用户切换远离“/ charts /:chartID”到“/ main”。我只想被调用一次,如果可能的

在App.js的方法,我用生命周期方法:

componentDidUpdate(){ 
    Meteor.call('test',"update"); 
    } // prints "update" 

    componentWillUnmount(){ 
    Meteor.call('test',"unmount"); 
    } // nothing happens when url changes 

Main.js(服务器):

Meteor.methods({ 
    'test'(obj){ 
     console.log(obj) 
    } 
}) 

我读从这个source组件应该卸载时,在我的情况下,URL的变化,但似乎我可能错过了一些东西。我应该如何解决这个问题?

任何帮助,非常感谢!

回答

0

您可以通过使用react-redux,浏览器本地存储或其他方式以不同方式完成此操作。在react-redux您可以更改您的商店。

例如,您可以在存储中定义“firstTimeInCharts”,并在“APP”的componentWillMount上更新它,并设置“firstTimeInMain”并在“MainPage”组件的componentWillMount上更新它。你可以跟踪是否通过检查这些状态来调用你的函数。您也可能希望在商店中存储其他值,以便在整个应用中访问它们。

或者,您可以使用浏览器的本地平台来做同样的事情。

我希望它能帮助你。