英文:
Setting Navigation Value Based On What The Function Returns
问题
以下是翻译好的部分:
当我调用登录函数时,我收到以下错误:
错误:在使用对象作为参数调用导航时,您需要指定名称或键。请参阅 https://reactnavigation.org/docs/navigation-actions#navigate 以获取用法。
这是我的代码:
login.jsx
<TouchableOpacity
style={styles.button}
onPress={() => navigation.navigate(this.onClick())}>
<Icon
style={styles.buttonIcon}
name="arrow-right"
size={30}
color="white"
type="feather"
/>
</TouchableOpacity>
这是函数:
ParticleAuth.js
onClick = async () => {
this.init();
this.setLanguage();
this.setChainInfo();
this.login();
const result = await particleAuth.isLogin();
console.log('Result:', result);
if (result) {
return "LoggedIn"; // LoggedIn和Error是屏幕的名称
} else {
return "Error";
}
};
根据错误信息,我需要预设navigation.navigate
的值。以下是一些可能的替代代码:
谢谢!
英文:
When I call a login function, this is the error I get:
Error: You need to specify name or key when calling navigate with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigate for usage.
This is my code:
login.jsx
<TouchableOpacity
style={styles.button}
onPress={() => navigation.navigate(this.onClick())}>
<Icon
style={styles.buttonIcon}
name="arrow-right"
size={30}
color="white"
type="feather"
/>
This is the function:
ParticleAuth.js
onClick = async () => {
this.init();
this.setLanguage();
this.setChainInfo();
this.login();
const result = await particleAuth.isLogin();
console.log('Result:', result);
if (result) {
return "LoggedIn"; // LoggedIn & Error are names of screens
} else {
return "Error";
}
};
According to the error, I have to preset the value for navigation.navigate
. What might be some alternate code to mine?
Thanks!
答案1
得分: 1
你可以将你的 navigation
移动到 onClick()
函数中,并在其中导航到屏幕,如下所示:
<TouchableOpacity
style={styles.button}
onPress={this.onClick}>
<Icon
style={styles.buttonIcon}
name="arrow-right"
size={30}
color="white"
type="feather"
/>
</TouchableOpacity>
onClick = async () => {
this.init();
this.setLanguage();
this.setChainInfo();
this.login();
const result = await particleAuth.isLogin();
console.log('Result:', result);
// 修改这些行
let screenName = '';
if (result) {
screenName = "LoggedIn"; // LoggedIn 和 Error 是屏幕的名称
} else {
screenName = "Error";
}
navigation.navigate(screenName);
};
英文:
You can move your navigation
to onClick()
function and navigate to the screen in there as below:
<TouchableOpacity
style={styles.button}
onPress={this.onClick}>
<Icon
style={styles.buttonIcon}
name="arrow-right"
size={30}
color="white"
type="feather"
/>
onClick = async () => {
this.init();
this.setLanguage();
this.setChainInfo();
this.login();
const result = await particleAuth.isLogin();
console.log('Result:', result);
// change this lines
let screenName = '';
if (result) {
screenName = "LoggedIn"; // LoggedIn & Error are names of screens
} else {
screenName = "Error";
}
navigation.navigate(screenName);
};
答案2
得分: 0
我找到了解决方法。这不是最高效的方法,但我使用了一个名为“加载中”的中间屏幕,当登录功能工作时。
英文:
I found the solution. It's not the most efficient, but I used an intermediary screen called Loading while the login function works
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论