如何在React Native中使用Navigator转换到组件的方法?

时间:2015-12-16 17:00:12

标签: javascript reactjs react-native

我的主导航组件如下所示:

onTransitionedTo

我想在页面组件转换到导航器中时调用一个方法(类似 changeScene(data) { if (this.refs.navigator.getCurrentRoutes().find(r => r.title === data.title)) this.refs.navigator.jumpTo(data); else this.refs.navigator.push(data); this.setState({currentView: data.title}); if (data.component.onTransitionedTo) data.component.onTransitionedTo(); } )。我尝试了以下方法:

rating

但这显然不起作用。什么是实现这一目标的最佳方式?

1 个答案:

答案 0 :(得分:0)

也许尝试在componentWillMount或其他一些适当的生命周期方法上执行传递的回调,以便组件检查它是否已转换到,而不是直接尝试在子项上执行某些操作的父组件。

在Main:

server: http://localhost:9876

load:
 - C:/Sonar/sonar-runner-dist-2.4/sonar-runner-2.4/projects/sources/*.js
 - C:/Sonar/sonar-runner-dist-2.4/sonar-runner-2.4/projects/sources/com/company/*.js
test:
 - C:/Sonar/sonar-runner-dist-2.4/sonar-runner-2.4/projects/tests/*.js
 - C:/Sonar/sonar-runner-dist-2.4/sonar-runner-2.4/projects/tests/com/company/*.js

plugin:
 - name: "coverage"
   jar: "coverage-1.3.5.jar"
   module: "com.google.jstestdriver.coverage.CoverageModule"

在组件中:

React.createElement(PageOne, { isActive: () => this._isActive(PageOne) })
...
_isActive(component) {
    let routes = this.refs.navigator.getCurrentRoutes();
    return component === routes[routes.length - 1];
}