react-native Navigation导航器,这原因我服了

本文介绍了如何在ReactNative应用中通过`navigate`和`goBack`传递参数到前后页面,自定义header样式和功能,以及使用`Tab.Navigator`进行嵌套导航,包括设置图标和监听Tab的点击事件。
摘要由CSDN通过智能技术生成
2.5.3 传递参数到之前的页面

不仅仅能传递参数到新的页面,也能传递参数到之前的页面。

想做到这个,你可以使用navigate的方法,如果页面存在的话,可以使用像 goBack 这样的方法。你可以通过navigate携带参数将参数传回去:

// Some.js
import React, { useState, Component } from “react”;
import { Text, View } from “react-native”;

class App extends Component {
constructor(props) {
super(props)
}
render() {
return (

{this.props.route.params.type ? this.props.route.params.type : “has null”}

)
}
}
export default App;

// Home.js
import * as React from ‘react’;
import { View, TextInput, Button } from ‘react-native’;
function goLogin(navigation, postText) {
navigation.push(“Login”, {type: postText}) // 把输入框的值传递给 Login 页面
}
function Some({navigation, route}) {
let {text} = route.params;
let [postText, setPostText] = React.useState(text);
return (

<TextInput
multiline
placeholder=“What’s on your mind?”
style={{ height: 200, padding: 10, backgroundColor: ‘gray’ }}
value={postText}
onChangeText={setPostText}
/>
<Button
title=“Go to Login”
onPress={() => goLogin(navigation, postText)}
/>

);
}
export default Some;

2.6 配置header bar

自定义 header 样式有3个关键属性:headerStyle, headerTintColor ,和 headerTitleStyle

function App() {
return (

<Stack.Navigator>
<Stack.Screen
name=“Home”
component={Home}
options={{
title: ‘My home’,
headerStyle: {
backgroundColor: ‘#f4511e’,
},
headerTintColor: ‘#fff’,
headerTitleStyle: {
fontWeight: ‘bold’,
},
}}
/>
</Stack.Navigator>

);
}

2.7 Header Button

给 header 右侧添加一个操作按钮–常见功能

function App() {
return (

<Stack.Navigator initialRouteName=“Login”>
<Stack.Screen options={{headerShown: false}} name=“Login” component={Login} />
<Stack.Screen name=“Some” component={Some}
options={{
headerRight: () => (
<Button
onPress={() => alert(‘This is a button!’)}
title=“add”
color=“#999”
/>
),
}}
/>
</Stack.Navigator>

);
}

3. 嵌套导航(Tabs)

3.1 安装依赖

npm install @react-navigation/bottom-tabs // 5.x版本

3.2 Example

// Some.js // 将之前的 Some.js 替换为以下代码
import {createBottomTabNavigator} from ‘@react-navigation/bottom-tabs’;
import * as React from ‘react’;
import { View, Button, Text } from ‘react-native’;

const Tab = createBottomTabNavigator(); //

function HomePage() {
return (
<View
style={{
flex: 1,
backgroundColor: ‘#ccc’,
justifyContent: ‘center’,
alignItems: ‘center’,
}}>
HomePage
<Button
title=“Go to Profile”
onPress={() => navigation.push(‘Login’)}
/>

);
}

function Detail({navigation}) {
return (
<View
style={{
flex: 1,
backgroundColor: ‘#ddd’,
justifyContent: ‘center’,
alignItems: ‘center’,
}}>
Detail

);
}
function Mine({navigation}) {
return (
<View
style={{
flex: 1,
backgroundColor: ‘#ddd’,
justifyContent: ‘center’,
alignItems: ‘center’,
}}>
Mine

);
}
function Some({navigation, route}) {
return (
<Tab.Navigator>
<Tab.Screen name=“HomePage” component={HomePage} />
<Tab.Screen name=“Detail” component={Detail} />
<Tab.Screen name=“Mine” component={Mine} />
</Tab.Navigator>
);
}
export default Some;

3.3 给 Tab 添加 icon

第一种

function TabNav() {
return (
<Tab.Navigator
initialRouteName=“HomePage”
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let icon;
if (route.name === ‘HomePage’) {
icon = focused
? require(“@/assets/images/icon_tab1_active.png”)
: require(“@/assets/images/icon_tab1.png”);
} else
if (route.name === ‘Detail’) {
icon = focused
? require(“@/assets/images/icon_tab2_active.png”)
: require(“@/assets/images/icon_tab2.png”);
}else if (route.name === ‘Mine’) {
icon = focused
? require(“@/assets/images/icon_tab3_active.png”)
: require(“@/assets/images/icon_tab3.png”);
}
return
},
})}

<Tab.Screen name=“HomePage” component={Home} />
<Tab.Screen name=“Detail” component={Geo} />
<Tab.Screen name=“Mine” component={Mine} />

</Tab.Navigator>
);
}

第二种 reactnavigation.org/docs/bottom…

function MyTabBar({ state, descriptors, navigation }) {
const focusedOptions = descriptors[state.routes[state.index].key].options;

if (focusedOptions.tabBarVisible === false) {
return null;
}

return (
<View style={{ flexDirection: ‘row’ }}>
{
state.routes.map((route, index) => {
const { options } = descriptors[route.key];
const label =
options.tabBarLabel !== undefined
? options.tabBarLabel
: options.title !== undefined
? options.title
: route.name;

const isFocused = state.index === index;

let icon;
if (label === ‘HomePage’) {
icon = isFocused
? require(“@/assets/images/icon_tab1_active.png”)
: require(“@/assets/images/icon_tab1.png”);
} else
if (label === ‘Detail’) {
icon = isFocused
? require(“@/assets/images/icon_tab2_active.png”)
: require(“@/assets/images/icon_tab2.png”);
}else if (label === ‘Mine’) {
icon = isFocused
? require(“@/assets/images/icon_tab3_active.png”)
: require(“@/assets/images/icon_tab3.png”);
}

const onPress = () => {
const event = navigation.emit({
type: ‘tabPress’,
target: route.key,
canPreventDefault: true,
});
if (!isFocused && !event.defaultPrevented) {
navigation.navigate(route.name);
}
};

const onLongPress = () => {
navigation.emit({
type: ‘tabLongPress’,
target: route.key,
});
};

return (
<TouchableOpacity
activeOpacity={0.8}
accessibilityRole=“button”
accessibilityState={isFocused ? { selected: true } : {}}
accessibilityLabel={options.tabBarAccessibilityLabel}
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}
style={{ flex: 1, paddingBottom: 8, paddingTop:8, flexDirection:“column”,justifyContent:“center”,alignItems:“center”, backgroundColor:“#fff” }}

function Some({navigation, route}) {
return (
<Tab.Navigator
tabBar={props => <MyTabBar {…props} />}

<Tab.Screen name=“HomePage” component={HomePage} />
<Tab.Screen name=“Detail” component={Detail} />
<Tab.Screen name=“Mine” component={Mine} />
</Tab.Navigator>
);
}
export default Some;

3.4 监听Tab的点击

class Home extends Component {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

更多学习和讨论,欢迎加入我们!

有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

这里有2000+小伙伴,让你的学习不寂寞~·

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

p.com/2024/03/13/H4lCoPEF.jpg" />

[外链图片转存中…(img-dvpVziQ3-1712793130125)]

更多学习和讨论,欢迎加入我们!

有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

这里有2000+小伙伴,让你的学习不寂寞~·

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值