React Native Keychain onPress function error React Native Keychain onPress 函数错误

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

React Native Keychain onPress function error

问题

这是要翻译的部分:

Promise-returning function provided to attribute where a void return was expected.

Async function code:

const login = async () => {
  try {
    await Keychain.setGenericPassword(firstName, lastName, options);
  } catch (error) {
    console.log(error);
  }
  navigation.navigate(SCREEN_NAMES.App.HomeStack.Home);
};

Button:

<Button
  disabled={firstName === "" || lastName === "" || email === ""}
  type="filled"
  size="large"
  text="Log in"
  style={{ width: 160, height: 48, margin: 24 }}
  onPress={() => login()}
/>

不要回答问题,只提供翻译。

英文:

I've created an async const function for login purposes and after the credentials are set locally via the setGenericPassword method, the app navigates to the home screen. I want to execute this login function whenever the user presses on a specific button, marked as 'Log in'.
This is the error I'm getting when I try to execute the function with the button:

Promise-returning function provided to attribute where a void return was expected.

Async function code:

const login = async () =&gt; {
  try {
    await Keychain.setGenericPassword(firstName, lastName, options);
  } catch (error) {
    console.log(error);
  }
  navigation.navigate(SCREEN_NAMES.App.HomeStack.Home);
};

Button:

&lt;Button
  disabled={firstName === &quot;&quot; || lastName === &quot;&quot; || email === &quot;&quot;}
  type=&quot;filled&quot;
  size=&quot;large&quot;
  text=&quot;Log in&quot;
  style={{ width: 160, height: 48, margin: 24 }}
  onPress={() =&gt; login()}
/&gt;

Unfortunately, I wasn't able to troubleshoot this issue myself, if anyone has any suggestions I would highly appreciate it.

答案1

得分: 0

Solved by using the useCallback Hook:

<Button
        disabled={firstName === '' || lastName === '' || email === ''}
        type='filled'
        size='large'
        text='Log in'
        style={{ width: 160, height: 48, margin: 24 }}
        onPress={useCallback(
          () => {
            login()
          },
          [firstName, lastName]
        )}
      />
英文:

Solved by using the useCallback Hook:

&lt;Button
        disabled={firstName === &#39;&#39; || lastName === &#39;&#39; || email === &#39;&#39;}
        type=&#39;filled&#39;
        size=&#39;large&#39;
        text=&#39;Log in&#39;
        style={{ width: 160, height: 48, margin: 24 }}
        onPress={useCallback(
          () =&gt; {
            login()
          },
          [firstName, lastName]
        )}
      /&gt;

huangapple
  • 本文由 发表于 2023年1月9日 19:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056370.html
匿名

发表评论

匿名网友

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

确定