react-native - 导航到另一个页面传递变量
问题描述
我对 React Native 很陌生。我有以下页面,它从数据库中加载日期和结果,所有显示都正常:
import React, { useEffect, useState } from "react";
import {
Text,
FlatList,
StyleSheet,
TouchableOpacity,
Platform,
} from "react-native";
import AsyncStorage from "@react-native-community/async-storage";
import Colors from "../../../res/Colors/Colors";
import { s, scale } from "react-native-size-matters";
import Axios from "axios";
import moment from "moment";
import { SafeAreaView} from "react-navigation";
export default function HistoryScreen({ navigation }) {
const [userDetails, setUserDetails] = useState(null);
const [loading, setLoading] = useState(true);
const [Star, setStar] = useState(null);
const [people, setPeople] = useState(null);
useEffect(() => {
_getHistory();
}, []);
const expandMonth = (month,year) => {
console.log(month);
console.log(year);
}
const _getHistory = async () => {
let star = await AsyncStorage.getItem("star");
star = JSON.parse(star);
setStar(star);
var formData = new FormData();
formData.append("StarID", star.StarID);
Axios({
method: "post",
url: "**API URL**",
data: formData,
//headers: { "Content-Type": "multipart/form-data" },
})
.then(async (response) => {
var responseBody = response.data.detail;
setPeople(responseBody);
setLoading(false);
})
.catch((error) => {
console.log({ error });
});
};
if (loading)
return (
<SafeAreaView>
</SafeAreaView>
);
return (
<SafeAreaView
style={{
flex: 1,
backgroundColor: Colors.lightGrey,
paddingTop: Platform.OS === "android" ? 40 : 40,
justifyContent: "space-evenly",
}}
>
<Text style={styles.headerText}>History</Text>
<FlatList
//keyExtractor={(item) => item.id}
data={people}
renderItem={({ item }) => (
<TouchableOpacity onPress={() => {
expandMonth(item.Month,item.Year)
navigation.navigate("HistoryMonthScreen", {
Month: item.Month,
Year: item.Year
});
}}
style={styles.month}>
<Text style={styles.monthname}>{moment(item.Month, 'M').format('MMMM')} {item.Year}</Text>
<Text style={styles.monthcount}>{item.Count} Bookings</Text>
</TouchableOpacity>
)}
/>
</SafeAreaView>
);
}
但是,当单击该TouchableOpacity
对象时,它不会导航到传递年份和月份变量的下一页。我假设我需要调用某种形式的导航,但是我尝试过的一切都导致了一个又一个错误。
解决方案
对于 React Navigation 5,您必须使用route
旁边传递的道具来navigation
导航到您要导航到的任何屏幕。
将以下代码放入HistoryMonthScreen.js
const HistoryMonthScreen = ({route, navigation}) => {
console.log(route.params.Month, route.params.Year);
return <View />
}
编辑:对于上下文:
我要离开你在这里的东西
navigation.navigate("HistoryMonthScreen", {
Month: item.Month,
Year: item.Year
});
所以我假设你在某个地方有一些 React Navigator,看起来像这样:
const AppNavigator5 = () => {
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="HistoryScreen"
screenOptions={{
headerShown: false,
animationEnabled: false,
}}>
<Stack.Screen
name="HistoryScreen"
component={HistoryScreen}
/>
<Stack.Screen
name="HistoryMonthScreen"
component={HistoryMonthScreen}
/>
<Stack.Screen name="Egg" component={Egg} />
</Stack.Navigator>
</NavigationContainer>
);
};
您将代码与route
该组件一起navigation
放入该HistoryMonthScreen
组件中。您可以将不同的参数传递给route.params
每次,您不必更改 HistoryMonthScreen。
推荐阅读
- java - Java 8 JRE 版本中哪些 jar 被列入黑名单?
- css - 在 CSS 中更改表格列标题和表格单元格元素位置 @media
- java - In what format is data sent on, getInputStream on a URLconnection Object?
- python - Convert timestamp to slots. Python. Pandas
- javascript - CSS网格 - 计算列数(javascript)
- mysql - 将每月成本转换为某个特定月份的每日成本
- c# - 如何在 C# 中使用正则表达式大写第一个字母(忽略非 az)?
- java - 无法访问 GraphNode 中的对象
- spotfire - Business Auther 中的“数据面板”许可证有什么作用?
- elasticsearch - Elasticsearch Copy_to 数据需要复制自己的子文档