sanniassin / react-input-mask

Input masking component for React. Made with attention to UX.
MIT License
2.22k stars 257 forks source link

Bugfix/prevent loss of reference #254

Open MtthsB opened 3 years ago

MtthsB commented 3 years ago

This merge request is intended to fix an insidious bug caused by a loss of reference to the input DOM element under certain circumstances. In concreto, when the input loses focus, i.e. on blur, in response to an action which causes a re-render of the component, the input (momentarily) loses its reference: the re-render causes the ref function invoked by React.createElement to re-initialise, passing a null object on first render. At the moment, this causes both the inputRef and the forwardedRef to be set to null.

In between the first render and the second invocation of the React.createElement ref function, onBlur (and the useValue function) is called on a null inputRef, causing a crash.

A simple fix is checking for the existence of the reference returned by the ref callback.

ianchanning commented 1 year ago

@MtthsB good catch - I've added this to my fork.