soywod / react-pin-field

📟 React component for entering PIN codes.
https://soywod.github.io/react-pin-field/
MIT License
412 stars 24 forks source link

Execute twice when strictmode #92

Open Mochamad-Rizky opened 6 months ago

Mochamad-Rizky commented 6 months ago

I'm having an issue where onComplete and other events are executing twice inside a React Strict Mode component called FormAuthPin.

My code:

export default function FormAuthPin({
  onCompletePin,
  isLoading,
  isError,
}: Props) {
  const pinRef = useRef<HTMLInputElement[]>([]);
  useEffect(() => {
    if (isError) {
      pinRef &&
        pinRef.current &&
        pinRef.current.forEach((input) => (input.value = ''));
    }
  }, [isError]);

  return (
    <PinField
      ref={pinRef}
      className={cn('pin-field', {
        complete: isLoading,
      })}
      autoFocus
      length={6}
      disabled={isLoading}
      onComplete={(value) => {
        console.log(value, 'value');
        // onCompletePin(value);
      }}
    />
  );
}

example (when i'm typing the last number): 123456

Result: (2x) 123456 value

soywod commented 6 months ago

I'm having an issue where onComplete and other events are executing twice inside a React Strict Mode component called FormAuthPin.

Thank you for the report, I will investigate as soon as I can.