【问题标题】:how to pass values to function from alert button in react native如何在本机反应中从警报按钮传递值以发挥作用
【发布时间】:2021-01-04 18:32:47
【问题描述】:

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

    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"),
                    },
                  ]
                );

【问题讨论】:

    标签: javascript reactjs react-native react-redux


    【解决方案1】:

    在 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"),
                        },
                      ]
                    );
    

    【讨论】:

      【解决方案2】:

      尝试消除

      "() =>" 来自 Yes 的 onPress 方法,如果不是,请尝试在 "() =>" 中添加参数,使其看起来像

      onPress: (e, h, p) => 这个.sendemail( e, H, p ),

      【讨论】:

        猜你喜欢
        • 2013-02-28
        • 1970-01-01
        • 1970-01-01
        • 2017-07-18
        • 2020-06-21
        • 2021-03-08
        • 1970-01-01
        • 2022-11-19
        • 2020-06-08
        相关资源
        最近更新 更多