简体   繁体   English

React Native 使用文本输入向 whatsapp 发送消息

[英]React Native send message to whatsapp with text Input

i'm trying to send message to whatsapp with text input,我正在尝试通过文本输入向whatsapp发送消息,

what i'm trying to do is input the mobile number -> type the message fully -> send the message by using Linking.openURL我要做的是输入手机号码-> 完整输入消息-> 使用 Linking.openURL 发送消息

but what the problem is when i try to type the message, when i haven't done with typing its directing me to the WhatsApp.但是当我尝试输入消息时,问题是什么,当我没有完成将我引导到 WhatsApp 的输入时。 i'll give you the picture我给你图片

在此处输入图像描述

as you've seen, its just 1 letter (the problem)如您所见,它只有 1 个字母(问题)

this is my function这是我的 function

sendOnWhatsApp = () => {
          console.log('test')
          let msg = this.state.msg;
          let mobile= this.state.mobile_no;

          if (mobile) {
               if (msg) {
                    let url = 'whatsapp://send?phone=62' + this.state.mobile_no + '&text=' + this.state.msg;
                    Linking.openURL(url).then(() => {
                         console.log('WhatasApp Opened');
                    }).catch(() => {
                         alert('Make Sure whatsapp is installed on your device');
                    });
               }else{
                    console.log('Please insert message to send');
               }
          }else{
               console.log('Please insert mobile no');
          }
     }

and this is my textInput这是我的文本输入

render() {
          return (
               <View style={styles.container}>
                    <Text style={{textAlign: 'center', fontSize: 20, paddingVertical: 30}}>
                          Send Your Message to Whatsapp
                    </Text>

                    <TextInput 
                         value={this.state.mobile_no}
                         onChangeText={mobile_no => this.setState({mobile_no})}
                         placeholder={'87877900297'}
                         style={styles.input}
                         keyboardType={'numeric'}
                    />
                    <TextInput 
                         value={this.state.msg}
                         onChangeText={msg => this.setState({msg})}
                         placeholder={'Enter Your Message'}
                         style={styles.input}
                    />

                    <View style={{marginTop: 20}}>
                         <Button 
                              onPress={this.sendOnWhatsApp()}
                              title={'Send Message'}
                         />
                    </View>
               </View>
          )
     }

how can i fix this to type fully and then send the message with the button?我该如何解决这个问题以完全输入,然后使用按钮发送消息?

thank you in advanced!提前谢谢你!

You're calling your function while passing it to onPress prop.您在将 function 传递给 onPress 道具时调用它。 You need to just pass the function reference instead.您只需传递 function 参考即可。

    onPress={this.sendOnWhatsApp}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM