首页 > 解决方案 > 如何在本机反应中从警报按钮传递值以发挥作用

问题描述

我正在尝试从警报按钮“是”调用并将值传递给我的函数,但它不起作用,当我使用函数值时,函数没有接收值并给出未定义的错误。这是我的代码

    const sendemail = async ({ email, hash, password }) => {
        console.log(email); //getting Error here undefined email
    }
      Alert.alert(
                  "Account is not activated",
                  "kindly check your email box and verify your account.Do you want to Resend Verification Email ?",
                  [
                    {
                      text: "Yes",
                      onPress: () =>
                        this.sendemail(
                          email,
                         hash,
                          password
                        ),
                    },
                    {
                      text: "No",
                      // onPress: () => console.log("No"),
                    },
                  ]
                );

标签: javascriptreactjsreact-nativereact-redux

解决方案


在 sendemail 调用中,您没有传递对象,而是传递了三个参数。然后你试图在方法中解构一个对象。试试这个:(我只删除了方法定义中的解构)

const sendemail = async ( email, hash, password ) => {
        console.log(email); 
    }
      Alert.alert(
                  "Account is not activated",
                  "kindly check your email box and verify your account.Do you want to Resend Verification Email ?",
                  [
                    {
                      text: "Yes",
                      onPress: () =>
                        this.sendemail(
                          email,
                         hash,
                          password
                        ),
                    },
                    {
                      text: "No",
                      // onPress: () => console.log("No"),
                    },
                  ]
                );

推荐阅读