Nav*_*ngh 1 javascript navigation react-native react-native-navigation
我想在路由参数中设置一个默认值,如果没有从我们以前做过的其他屏幕发送任何内容
let phot0 = this.props.navigation.getParam("photo","empty");
Run Code Online (Sandbox Code Playgroud)
在 react Navigation 5.x 中做什么我的代码是..(第 5 行的困难)
import React from "react";
import { StyleSheet, Text, View, Image, Button } from "react-native";
export default function Home({ route, navigation }) {
const { photo } = route.params;
return (
<View style={styles.container}>
<Image
resizeMode="center"
style={styles.imageHolder}
source={photo === "empty" ? require("../assets/email.png") : photo}
/>
<Button
title="take photo"
style={styles.button}
onPress={() => navigation.navigate("Camera")}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
imageHolder: {
alignSelf: "center",
},
button: {
margin: 20,
},
});
Run Code Online (Sandbox Code Playgroud)
它也显示了一些错误:未定义不是对象(评估'route.params.photo')..我是否总是需要在发送屏幕中声明参数?
您可以将一些初始参数传递给react-navigation 版本 5 中的屏幕,如下所示,
<Stack.Screen
name="Details"
component={DetailsScreen}
initialParams={{ itemId: 100 }}
/>
Run Code Online (Sandbox Code Playgroud)
根据示例,如果您在导航到Details屏幕时未指定任何参数,则将使用初始参数。
有关更多信息,请查看以下完整示例
import * as React from "react";
import { Text, View, Button } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
navigation.navigate("Details");
}}
/>
</View>
);
}
function DetailsScreen({ route, navigation }) {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>Details Screen</Text>
<Text>itemId: {route.params.itemId}</Text>
<Button title="Go back" onPress={() => navigation.goBack()} />
</View>
);
}
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen
name="Details"
component={DetailsScreen}
/**
* when you didn't specify itemId params the initial params will be used
*/
initialParams={{ itemId: 100 }}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
Run Code Online (Sandbox Code Playgroud)
希望这对你有帮助。如有疑问,请放心。
| 归档时间: |
|
| 查看次数: |
2690 次 |
| 最近记录: |