我正在探索React Native的可能性,同时在 Navigator 组件的帮助下开发一个在视图之间自定义导航的演示应用程序 - http://facebook.github.io/react-native/docs/navigator.html#content
主应用程序类呈现导航器,内部 renderScene
返回传递的组件:
class App extends React.Component {
render() {
return (
<Navigator
initialRoute={{name: 'WelcomeView', component: WelcomeView}}
configureScene={() => {
return Navigator.SceneConfigs.FloatFromRight;
}}
renderScene={(route, navigator) => {
// count the number of func calls
console.log(route, navigator);
if (route.component) {
return React.createElement(route.component, { navigator });
}
}}
/>
);
}
}
现在app包含2个视图:
class FeedView extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>
Feed View!
</Text>
</View>
);
}
}
class WelcomeView extends React.Component {
onPressFeed() {
this.props.navigator.push({
name: 'FeedView',
component: FeedView
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome View!
</Text>
<Text onPress={this.onPressFeed.bind(this)}>
Go to feed!
</Text>
</View>
);
}
}
我想弄清楚的是 -
- 我在日志中看到,当按下"go to feed"
renderScene
被多次调用时,虽然视图正确呈现一次 . 这是动画的工作原理吗?
index.ios.js:57 Object {name: 'WelcomeView', component: function}
index.ios.js:57 Object {name: 'FeedView', component: function}
// renders Feed View
- 一般来说,我的方法是否符合React方式,还是可以做得更好?
我想要实现的是类似于NavigatorIOS但没有导航栏(但是一些视图将有自己的自定义导航栏) .
4 回答
你的方法应该很好 . 在Fb的大型应用程序中,我们避免在渲染场景组件之前调用
require()
,这可以节省一些启动时间 .首次将场景推送到导航器时,应调用
renderScene
函数 . 当导航器重新渲染时,也会调用活动场景 . 如果你看到renderScene
在push
后被多次调用,那么它可能是一个bug .导航器仍在进行中,但如果您发现任何问题,请在github上提交并标记我! (@ericvicenti)
正如其他人之前提到的,Navigator自v0.44以来已被弃用,但仍可导入以支持旧应用程序:
根据文档(React Native v0.54)Navigating Between Screens . 现在建议您使用React Navigation(如果您刚刚开始使用),或者NavigatorIOS用于非Android应用程序
NB :在提供此答案时,React Native的版本为0.54
Navigator
现在在RN 0.44.0
中已弃用,您可以使用react-native-deprecated-custom-components
来支持使用Navigator
的现有应用程序 .导航器组件现已弃用 . 你可以使用来自askonov的react-native-router-flux,它有很多种类,支持许多不同的动画,效率很高