''otp' auto fetching not working in react native

react-native-otp-autocomplete and react-native-otp-verify not working as expected:

few scenarios which occurred in my case:-

  1. Throws Timeout Error when Otp is recieved.
  2. Sometimes does not reads message.
  3. Sometimes reads message and works fine but this the rare case.
      const getHash = () => OtpAutocomplete.getHash().then(console.log).catch(console.log);
    
      const startListeningForOtp = () =>
        OtpAutocomplete.getOtp()
          .then(p => OtpAutocomplete.addListener(otpHandler))
          .catch(p => console.log(p));
    
      const otpHandler = (message: string) => {
        console.log(message, 'message is');
        const otp = message?.substring(0, 4);
        console.log(otp, 'otp is');
        setVerificationCode(otp);
        OtpAutocomplete.removeListener();
      };
    
      useEffect(() => {
        getHash();
        startListeningForOtp();
        return () => OtpAutocomplete.removeListener();
      }, [startListeningForOtp]);

this is my code I've used both packages mentioned above.

I've also appended hash key into the otp message as required by the sms-retrieval-api



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source