gpt4 book ai didi

javascript - React-Native 导航栏如何工作

转载 作者:行者123 更新时间:2023-12-03 06:46:09 25 4
gpt4 key购买 nike

所以我一直在尝试获得 react 原生导航,我在网上查看了很多示例,但我仍然不太明白我做错了什么。

我的示例大致基于我在 Stackoverflow 上找到的另一个示例:

react-native Navigator.NavigationBar - where are the docs?

我无法弄清楚如何将变量传递到导航栏中,以获取“route.title”和“route.leftButton”等值。

当我第一次加载应用程序时,一切看起来都很好。它从 Navigator.initialRoute 属性获取数据,但是如果我在 Debug模式下单击左侧或右侧按钮并检查路由的值,我可以看到它是一个仅包含单个属性“id”的对象设置为“未定义”。

我已经查看了文档,我认为它可能太简短,我无法完全理解。对此的任何指导表示赞赏。

谢谢。

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Navigator,
TouchableOpacity
} from 'react-native';

var NavigationBarRouteMapper = {
LeftButton: function( route, navigator, index, navState ){
return(
<TouchableOpacity onPress={() => navigator.pop()}>
<Text>{ route.leftButton }TestLeft</Text>
</TouchableOpacity>
)
},
Title: function( route, navigator, index, navState ){
return(
<Text>{ route.title }</Text>
)
},
RightButton: function( route, navigator, index, navState ){
debugger;
return(
<TouchableOpacity onPress={() => navigator.push({id: 'PageTwo', title:'page222'})}>
<Text>{ route.rightButtonAction }TestRight</Text>
</TouchableOpacity>
)
}
}

var PageOne = React.createClass({
render(){
return (
<View>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
<Text>you are on page 1</Text>
</View>
)
}
});

var PageTwo = React.createClass({
render(){
return (
<View>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
<Text>you are on page 2</Text>
</View>
)
}
});

class testApp extends Component {

renderScene( route, nav ) {
switch (route.id) {
case 'PageOne':
return <PageOne navigator={ nav } leftButton={ "Back" } title={ "PageOne111" } rightButtonAction={"PageTwo"} />
case 'PageTwo':
return <PageTwo navigator={ nav } leftButton={ "Back" } title={ "PageTwo222" } rightButtonAction={"PageOne"} />;
}
}

render() {
return (
<Navigator
initialRoute={{ id: 'PageOne', title: 'PageOne' }}
renderScene={ this.renderScene }
configureScene={( route ) => {
if ( route.sceneConfig ) {
return route.sceneConfig;
}
return Navigator.SceneConfigs.FloatFromRight;
}}
navigationBar={
<Navigator.NavigationBar
routeMapper={ NavigationBarRouteMapper }
/>
}
/>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

AppRegistry.registerComponent('testApp', () => testApp);

最佳答案

我建议阅读这篇文章,因为它看起来内容非常丰富。这是今年二月份的,所以有些可能已经过时了。

https://medium.com/@dabit3/react-native-navigator-navigating-like-a-pro-in-react-native-3cb1b6dc1e30#.45yr7nycr

我认为这基本上可以归结为能够访问 NavigationBarRouteMapper 函数中的路线和导航属性,并使用这些属性来自定义值。

(从文章中复制的代码,以防它消失)

var NavigationBarRouteMapper = {
LeftButton(route, navigator, index, navState) {
if(index > 0) {
return (
<TouchableHighlight
underlayColor="transparent"
onPress={() => { if (index > 0) { navigator.pop() } }}>
<Text style={ styles.leftNavButtonText }>Back</Text>
</TouchableHighlight>)
}
else { return null }
},
RightButton(route, navigator, index, navState) {
if (route.onPress) return (
<TouchableHighlight
onPress={ () => route.onPress() }>
<Text style={ styles.rightNavButtonText }>
{ route.rightText || 'Right Button' }
</Text>
</TouchableHighlight>)
},
Title(route, navigator, index, navState) {
return <Text style={ styles.title }>MY APP TITLE</Text>
}
};

希望这能帮助您朝着正确的方向前进。

关于javascript - React-Native 导航栏如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37726703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com