uNmAnNeR / imaskjs

vanilla javascript input mask
https://imask.js.org
MIT License
4.96k stars 258 forks source link

The ref in useIMask triggers renders multiple times #1038

Closed npearson72 closed 6 months ago

npearson72 commented 6 months ago

Describe the bug Adding the ref to an input from the useIMask hook will cause the component to render 3 times. Admittedly I don't know what is happening within the iMask mounting process, but from a "React" perspective a ref is generally used to avoid triggering renders.

To Reproduce Using the following code will produce 3 renders when it's first loaded:

import { useIMask } from 'react-imask';

export const MyInput = () => {
  const { ref } = useIMask({
    mask: '000-`00-`0000',
    lazy: false
  });

  console.log('I am rendering....');

  return <input ref={ref} />;
};

Expected behavior I would expect the component to only render once.

Environment:

uNmAnNeR commented 6 months ago

i debug it and i think it's not possible. renders are:

  1. mount (ref is null)
  2. init ref, apply mask
  3. change internal state and external values (value/unmaskValue/typedValue)

I am not an expert in react so if you have any ideas let me know.