react-code-input icon indicating copy to clipboard operation
react-code-input copied to clipboard

Component is used as uncontrolled instead of controlled

Open chris-verclytte opened this issue 6 years ago • 3 comments

Hi,

I had some troubles trying to use the component as controlled. My use case is quite simple : I want to use the component on mobile and to only allow numbers to be entered by using a 'numeric' dynamic keyboard. After trying all mixes of type, inputMode and pattern. I finally realized that no one would work (here is a demo) for a number password and that the correct combination to implement this would be to put type="tel" (to properly display a keyboard with only numbers) and to use a class to set -webkit-text-security: disc in order to hide the password.

But then, the resulting HTML will no longer filter key for the numeric constraint. I decided to handle it myself by filtering code on change by simply calling an HOC when the code matches [0-9]* regex. The problem is that the component finally ignore the value passed in props but uses its local state. Then value prop and local state goes out of sync.

I think this is falling in the anti-patterns that React team describe in this blog post: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#anti-pattern-unconditionally-copying-props-to-state

chris-verclytte avatar Oct 22 '18 14:10 chris-verclytte

+1

ShonP avatar Aug 15 '19 07:08 ShonP

+1

kembala avatar Oct 07 '19 03:10 kembala

+1

lvillacin avatar Oct 11 '22 03:10 lvillacin