I am trying to use React hook form with NumberFormat without Controller and without ReactDOM.findDOMNode (which is deprecated and discouraged). The following code works
import React from 'react';
import ReactDOM from 'react-dom';
import { useForm } from 'react-hook-form';
import NumberFormat from 'react-number-format';
function FormWorking() {
const { register, handleSubmit, getValues } = useForm();
function onSubmit() {
console.log(getValues());
}
const cardNumberReg = register('cardNumber');
return (
<form onSubmit={handleSubmit(onSubmit)}>
<NumberFormat
format="#### #### #### ####"
{...cardNumberReg}
ref={(inst) => (cardNumberReg.ref(ReactDOM.findDOMNode(inst)))}
/>
</form>
);
}
ReactDOM.render(
<FormWorking />, document.getElementById('root')
);
but I don't want to use findDOMNode. None of the below attempts is working (cardNumber is blank or undefined in the log)
<NumberFormat
format="#### #### #### ####"
{...cardNumberReg}
getInputRef={(e: any) => (cardNumberReg.ref(e))}
/>
<NumberFormat
format="#### #### #### ####"
{...cardNumberReg}
ref={cardNumberReg.ref}
/>
<NumberFormat
format="#### #### #### ####"
{...cardNumberReg}
ref={(e) => cardNumberReg.ref(e)}
/>
I am new to react and any help would be greatly appreciated.