我在 Expo 中收到以下错误:“在使用对象作为参数调用导航时,您需要指定名称或键。”
我有一段调用 LoginOptions 的主要代码包括:
<LoginOptions title="Don't have a business account?" link="Create a free account" press="CreateAccount"/>
我试图将导航目的地作为“变量?? - 不确定正确的词是什么”。 LoginOptions 看起来像这样:
import React from 'react';
import { View, Text, TouchableWithoutFeedback } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import defaultStyles from "../../config/styles";
function LoginOptions ({title, link, press }) {
const navigation = useNavigation();
return (
<View>
<Text style={defaultStyles.logBold}>
{title}
</Text>
<TouchableWithoutFeedback onPress={() => navigation.navigate({press})} >
<Text style={defaultStyles.logUnder} >
{link}
</Text>
</TouchableWithoutFeedback>
</View>
);
}
export default LoginOptions;
最佳答案
将其作为 loginOption 的属性传递
<LoginOptions navigation={navigation} title="Don't have a business account?" link="Create a free account" press={"CreateAccount"}/>
然后
import React from 'react';
import { View, Text, TouchableWithoutFeedback } from 'react-native';
import defaultStyles from "../../config/styles";
function LoginOptions ({title, link, press ,navigation}) {
return (
<View>
<Text style={defaultStyles.logBold}>
{title}
</Text>
<TouchableWithoutFeedback onPress={() => navigation.navigate(press)} >
<Text style={defaultStyles.logUnder} >
{link}
</Text>
</TouchableWithoutFeedback>
</View>
);
}
export default LoginOptions;
关于react-native - 在 React Native 导航中指定 onPress 的名称或键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65476162/