根据函数返回值设置导航数值。

huangapple go评论59阅读模式
英文:

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

&lt;TouchableOpacity
                style={styles.button}
                onPress={() =&gt; navigation.navigate(this.onClick())}&gt;
                &lt;Icon
                  style={styles.buttonIcon}
                  name=&quot;arrow-right&quot;
                  size={30}
                  color=&quot;white&quot;
                  type=&quot;feather&quot;
                /&gt;

This is the function:

ParticleAuth.js

onClick = async () =&gt; {
  this.init();
  this.setLanguage();
  this.setChainInfo();
  this.login();
  const result = await particleAuth.isLogin();
  console.log(&#39;Result:&#39;, result);
  if (result) {
    return &quot;LoggedIn&quot;; // LoggedIn &amp; Error are names of screens
  } else {
    return &quot;Error&quot;;
  }
};

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:

&lt;TouchableOpacity
                style={styles.button}
                onPress={this.onClick}&gt;
                &lt;Icon
                  style={styles.buttonIcon}
                  name=&quot;arrow-right&quot;
                  size={30}
                  color=&quot;white&quot;
                  type=&quot;feather&quot;
                /&gt;
onClick = async () =&gt; {
  this.init();
  this.setLanguage();
  this.setChainInfo();
  this.login();
  const result = await particleAuth.isLogin();
  console.log(&#39;Result:&#39;, result);

  // change this lines
  let screenName = &#39;&#39;;
  if (result) {
    screenName = &quot;LoggedIn&quot;; // LoggedIn &amp; Error are names of screens
  } else {
    screenName = &quot;Error&quot;;
  }
  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

huangapple
  • 本文由 发表于 2023年2月19日 14:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定