之前copy别人的代码,看 Navigator 使用起来还是很简单的,可是当自己亲自去coding的时候,感觉完全不是一回事,这篇博客记录自己遇到的坑!
问题一:使用 navigator.push 点击跳转后,手指滑动可以返回上一页,但是打log发现并没有调用 pop 方法,而且还是可以滑动回去,根本原因暂不追究,上代码:
<span style="font-size:18px;">_addNavigator(component, title){ var data = null; if(title === '首页'){ data = this.state.data; } return <Navigator style={{flex:1}} initialRoute={{ component: component, title: title, passProps:{ data: data } }} </span><pre name="code" class="javascript"><span style="font-size:18px;">configureScene={(route) => { return Navigator.SceneConfigs.HorizontalSwipeJumpFromRight; }}</span>
<span style="font-size:18px;">configureScene={(route) => { return Navigator.SceneConfigs.HorizontalSwipeJumpFromRight; }}</span>
详细配置请看源代码的: node_modules/react-native/Libraries/CustomComponents/Navigator/NavigatorSceneConfigs.js
HorizontalSwipeJumpFromRight: { ...BaseConfig, gestures: { jumpBack: { ...BaseRightToLeftGesture, overswipe: BaseOverswipeConfig, edgeHitWidth: null, isDetachable: true, }, jumpForward: { ...BaseLeftToRightGesture, overswipe: BaseOverswipeConfig, edgeHitWidth: null, isDetachable: true, }, pop: BaseRightToLeftGesture, }, animationInterpolators: { into: buildStyleInterpolator(FromTheLeft), out: buildStyleInterpolator(FadeToTheRight), }, },关键就在 gestures:{} 里面,我们可以在源码里找到:
FadeAndroid: { ...BaseConfig, gestures: null, animationInterpolators: { into: buildStyleInterpolator(FadeIn), out: buildStyleInterpolator(FadeOut), }, },
问题二:Navigator传递参数问题:
MainPage.js中:
_addNavigator(component, title){ var data = null; if(title === JOURNEY_TAB){ data = this.state.data; } return <Navigator style={{flex:1}} initialRoute={{ component: component, title: title, passProps:{ data: data } }} // configureScene={(route) => { // return Navigator.SceneConfigs.HorizontalSwipeJumpFromRight; // }} renderScene={(route, navigator) => { let Component = route.component; return <Component {...route.params} navigator={navigator} /> }}/>; }
selectJourney(journey: Object) { const {navigator} = this.props; if(navigator){ navigator.push({ name: 'JourneyDetail', component: JourneyDetail, params: { journeyData: journey, } }); } }
JourneyDetail.js中,点击返回按钮:
_pressBackButton(){ const { navigator } = this.props; if(navigator) { navigator.pop(); } }
因为看到在MainPage.js中:
renderScene={(route, navigator) => { let Component = route.component; return <Component {...route.params} navigator={navigator} /> }然后后面的几个页面全部都可以通过
const { navigator } = this.props;
获取到navigator,而且MainPage.js中的Component就是对应的TabNavigator上的几个页面,所以在想,是不是在
<Component {...route.params} navigator={navigator} />上定义的属性都是可以在之后的几个页面的js上获取到呢?事实证明猜想是对的,于是我把MainPage.js修改为:
_addNavigator(component, title){ var data = null; if(title === JOURNEY_TAB){ data = this.state.data; } return <Navigator style={{flex:1}} initialRoute={{ component: component, title: title, passProps:{ data: data } }} // configureScene={(route) => { // return Navigator.SceneConfigs.HorizontalSwipeJumpFromRight; // }} renderScene={(route, navigator) => { let Component = route.component; return <Component {...route.params} show={(is_show) => { this.setState({showTabBar: is_show}); }} navigator={navigator} /> }}/>; }
Journey.js中,在跳转到详细页面时候,改变状态:
selectJourney(journey: Object) { const {navigator,show} = this.props; show(false); console.log(show); if(navigator){ navigator.push({ name: 'JourneyDetail', component: JourneyDetail, params: { journeyData: journey, show: function(is_show){ show(is_show); } } }); } }
JourneyDetail.js中:
_pressBackButton(){ const { navigator } = this.props; if(this.props.show){ this.props.show(true); } if(navigator) { navigator.pop(); } }
this.props.show(true);