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

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

React Native Keychain onPress function error

问题

这是要翻译的部分:

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

Async function code:

  1. const login = async () => {
  2. try {
  3. await Keychain.setGenericPassword(firstName, lastName, options);
  4. } catch (error) {
  5. console.log(error);
  6. }
  7. navigation.navigate(SCREEN_NAMES.App.HomeStack.Home);
  8. };

Button:

  1. <Button
  2. disabled={firstName === "" || lastName === "" || email === ""}
  3. type="filled"
  4. size="large"
  5. text="Log in"
  6. style={{ width: 160, height: 48, margin: 24 }}
  7. onPress={() => login()}
  8. />

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

英文:

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:

  1. const login = async () =&gt; {
  2. try {
  3. await Keychain.setGenericPassword(firstName, lastName, options);
  4. } catch (error) {
  5. console.log(error);
  6. }
  7. navigation.navigate(SCREEN_NAMES.App.HomeStack.Home);
  8. };

Button:

  1. &lt;Button
  2. disabled={firstName === &quot;&quot; || lastName === &quot;&quot; || email === &quot;&quot;}
  3. type=&quot;filled&quot;
  4. size=&quot;large&quot;
  5. text=&quot;Log in&quot;
  6. style={{ width: 160, height: 48, margin: 24 }}
  7. onPress={() =&gt; login()}
  8. /&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:

  1. <Button
  2. disabled={firstName === '' || lastName === '' || email === ''}
  3. type='filled'
  4. size='large'
  5. text='Log in'
  6. style={{ width: 160, height: 48, margin: 24 }}
  7. onPress={useCallback(
  8. () => {
  9. login()
  10. },
  11. [firstName, lastName]
  12. )}
  13. />
英文:

Solved by using the useCallback Hook:

  1. &lt;Button
  2. disabled={firstName === &#39;&#39; || lastName === &#39;&#39; || email === &#39;&#39;}
  3. type=&#39;filled&#39;
  4. size=&#39;large&#39;
  5. text=&#39;Log in&#39;
  6. style={{ width: 160, height: 48, margin: 24 }}
  7. onPress={useCallback(
  8. () =&gt; {
  9. login()
  10. },
  11. [firstName, lastName]
  12. )}
  13. /&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:

确定