英文:
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 () => {
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()}
/>
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:
<Button
disabled={firstName === '' || lastName === '' || email === ''}
type='filled'
size='large'
text='Log in'
style={{ width: 160, height: 48, margin: 24 }}
onPress={useCallback(
() => {
login()
},
[firstName, lastName]
)}
/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论