2017-08-03 68 views

回答

-1

我发现黑客:

<Navigator 
    onNavigationStateChange={(prevState, currentState) => { 
    const getCurrentRouteName = (navigationState) => { 
    if (!navigationState) return null; 
    const route = navigationState.routes[navigationState.index]; 
    if (route.routes) return getCurrentRouteName(route); 
    return route.routeName; 
    }; 
    let currentRoute = getCurrentRouteName(currentState); 
    if(currentRoute == "A") { 
    console.log("TRIGGER") 
}}/>; 

然后你听日志中您的组件,如:

componentDidMount() { 
    let slf = this; 
    let log = console.log; 
    console.log = function(txt) { 
     if(txt == "TRIGGER") { 
     slf.myFunction(); // CALL HERE YOUR FUNCTION 
     } else { 
     log.apply(console, arguments); 
     } 
    } 
    } 
1

React-Navigation提供了一个onNavigationStateChange道具,您可以在导航状态更改时使用它来调用方法。 screen tracking example向您展示了一个可以获取新状态的密钥和路由名称的示例。

从例如,在getCurrentRouteName方法,你可以做const route = navigationState.routes[navigationState.index];一旦你retrived的route,你可以找到通过调用route.keyroute.routeName密钥和路由名称。

+0

有问题,你可以帮助请(注释行)?非常感谢你 https://jsfiddle.net/gdydL0p4/ –

+1

@ScoopDevek为此,我没有看到通过当前React-Navigation implmenetaion的直接解决方案,我的同事指出这个插件:https:// github .com/satya164/react-navigation-addons,你可以在这里阅读关于此功能的讨论:https://github.com/react-community/react-navigation/issues/51 – dotcomXY

+0

谢谢,我会发布如果我找到解决方案 –

相关问题