React Native 根据函数返回的内容设置导航值

lzfw57am  于 2023-02-19  发布在  React
关注(0)|答案(2)|浏览(138)

当我调用一个登录函数时,我得到的错误如下:

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.

这是我的代码:
login.jsx

<TouchableOpacity
                style={styles.button}
                onPress={() => navigation.navigate(this.onClick())}>
                <Icon
                  style={styles.buttonIcon}
                  name="arrow-right"
                  size={30}
                  color="white"
                  type="feather"
                />

这是一个函数:
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";
  }
};

根据错误,我必须预设navigation.navigate的值。什么可能是一些替代代码来挖掘?
谢谢!

xlpyo6sf

xlpyo6sf1#

我找到了解决方案。它不是最有效的,但我使用了一个名为Loading的中间屏幕,同时登录功能正常工作

rseugnpd

rseugnpd2#

您可以将navigation移动到onClick()功能,并导航到其中的屏幕,如下所示:

<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);
};

相关问题