React Native OTP Autofill Autocomplete TextField Component Using react-native-otp-autocomplete Library in Javascript Full Project For Beginners

 

 

npm i react-native-otp-autocomplete

 

 

Usage

import OtpAutocomplete from 'react-native-otp-autocomplete';

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) => {
        const otp = /(\d{4})/g.exec(message)[1];
        this.setState({ otp });
        OtpAutocomplete.removeListener();
        Keyboard.dismiss();
}

useEffet(() => {
    startListeningForOtp();

    return () => OtpAutocomplete.removeListener();
}, [startListeningForOtp]);

Leave a Reply