Skip to content

Commit 74c9a55

Browse files
author
weimeng
committed
增加深入使用Navigator的示例
1 parent 85b2b6a commit 74c9a55

3 files changed

Lines changed: 116 additions & 2 deletions

File tree

app/domain/page/Example2.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@ export class Example2 extends Component{
3535
_onPress(){
3636
this.props.navigator.pop()
3737
}
38+
39+
_onNext(){
40+
41+
this.props.navigator.push({name : "Example3"})
42+
}
3843
render(){
3944

4045

4146
return <View style={{flex : 1, ...flexCenter, backgroundColor : "yellow"}}>
4247

4348
<Text>页面Example2</Text>
49+
4450
<ZButton onPress={this._onPress.bind(this)}>返回Example1</ZButton>
51+
52+
<View style={{marginTop : 10}}>
53+
<ZButton onPress={this._onNext.bind(this)}>Next</ZButton>
54+
</View>
4555
</View>
4656
}
4757
}

app/domain/page/Example3.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/***********************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2016 珠峰课堂,Ramroll
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*
23+
*/
24+
25+
import React, {Component} from 'react'
26+
27+
import {View, Text} from 'react-native'
28+
29+
import {ZButton} from "domain/component"
30+
import {flexCenter} from "basic"
31+
32+
33+
export class Example3 extends Component{
34+
35+
_onPress(){
36+
// POP 两次 --- 会连续跳两张页面
37+
// 体验很糟糕
38+
// this.props.navigator.pop()
39+
// this.props.navigator.pop()
40+
41+
42+
// 可以
43+
// 但是很难确定应该pop几张页面
44+
// 程序不好维护
45+
// this.props.navigator.popN(2)
46+
47+
48+
/** 方法1 **/
49+
// 拿到RouteStack
50+
// const routes = this.props.navigator.getCurrentRoutes()
51+
// Es6的语法, find方法传入一个prediction
52+
// const routeTo = routes.reverse().find(v => v.name === "Example1")
53+
// this.props.navigator.popToRoute(routeTo)
54+
55+
56+
/** 方法2 **/
57+
// const routes = this.props.navigator.getCurrentRoutes()
58+
// const routeTo = routes.find(v => v.name === "Example1")
59+
// this.props.navigator.resetTo(routeTo)
60+
61+
62+
/** 方法3 - 万不得以 **/
63+
64+
//[Example1, Example2, Example3] -> [Example1, Example3]
65+
//有性能问题
66+
this.props.navigator.immediatelyResetRouteStack([{name : "Example1"}, {name : "Example3"}])
67+
68+
// 有时序问题
69+
// 此处在特定机型上可能触发bug,在10ms内 immediatelyResetRouteStack没有执行完成
70+
setTimeout( (() => {
71+
// [Example1, Example2] -> [Example1]
72+
this.props.navigator.pop()
73+
}).bind(this), 10)
74+
75+
}
76+
77+
78+
render(){
79+
80+
81+
return <View style={{flex : 1, ...flexCenter, backgroundColor : "yellow"}}>
82+
<Text>页面Example3</Text>
83+
<ZButton onPress={this._onPress.bind(this)}>成功</ZButton>
84+
</View>
85+
}
86+
}
87+
88+
// 到了Example3 路由栈 [Example1, Example2, Example3]
89+
// 期望"成功"按钮返回Example1 ----> [Example1]

app/entry.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,31 @@ import React, {Component} from 'react'
2626

2727
import {
2828
View,
29-
Navigator
29+
Navigator,
30+
BackAndroid
3031
} from 'react-native'
3132

3233

3334
import {Example1} from 'domain/page/Example1'
3435
import {Example2} from 'domain/page/Example2'
36+
import {Example3} from 'domain/page/Example3'
37+
3538

3639
export class Entry extends Component {
3740

3841
constructor(){
3942
super()
4043
}
44+
45+
// 组件挂在后
46+
componentDidMount(){
47+
BackAndroid.addEventListener('hardwareBackPress', (() => {
48+
const navigator = this.refs.navigator
49+
navigator.pop()
50+
return true
51+
52+
}).bind(this));
53+
}
4154

4255
_renderScene(route, navigator){
4356

@@ -48,6 +61,8 @@ export class Entry extends Component {
4861
return <Example1 navigator={navigator} />
4962
case "Example2" :
5063
return <Example2 navigator={navigator} />
64+
case "Example3" :
65+
return <Example3 navigator={navigator} />
5166
}
5267

5368
}
@@ -58,7 +73,7 @@ export class Entry extends Component {
5873
// initialRoute 设置第一张页面
5974
// renderScene 绘制场景
6075
return <Navigator
61-
76+
ref="navigator"
6277
initialRoute={{name : "Example1"}}
6378
renderScene={this._renderScene}
6479

0 commit comments

Comments
 (0)