tttstudios / react-native-otp-input

Tiny Javascript library which provides an elegant UI for user to input one time passcode.
MIT License
521 stars 238 forks source link

onCodeFilled method called multiple times in iOS. #225

Open dprajapati1179 opened 8 months ago

dprajapati1179 commented 8 months ago

Describe the bug The onCodeFilled Callback function which triggers when all fields of the OTP have been filled is called 2-4 times due to which the functionality is braking in my app.

To Reproduce Steps to reproduce the behavior:

  1. Add one OTP input and add one console in onCodeFilled method.
  2. Fill the input completely add all six numbers.
  3. You will find that in iOS the console is executed multiple times.
  4. See error

Expected behavior It should work only one time once the code is filled as working on Android.

Smartphone (please complete the following information):

baraa-rasheed commented 8 months ago

@dprajapati1179 You're right for a quick fix, you can use the debounce function from lodash

import debounce from 'lodash/debounce';
import OTPInputView from '@twotalltotems/react-native-otp-input';

export default function App() {

  const onCodeFilled = debounce((code) => {
    sendCode(code, navigation);
  }, 500)

  return (
    <OTPInputView
      pinCount={4}
      onCodeFilled={onCodeFilled}
    />
  );

}
dprajapati1179 commented 3 months ago

sendCode(code, navigation);

You are right @baraa-rasheed But when we are calling API at that time it is calling the API after debounced time and due to that duplicate data is created sometimes