antd form.useWatch() error Hooks can only be called inside of the body of a function component

28 Views Asked by At

I am trying to access Form.useWatch() from antd in checkAccountNumber function but I am getting this error

Hooks can only be called inside of the body of a function component.

const getFieldValue = (fieldName,form) => {
    return Form.useWatch(fieldName,form);
 }

 const checkAccountNumber = () => {
    const accountNumber = getFieldValue("account_number", form)
    if(isNumeric(accountNumber))
        console.log("this true")
    else
        console.log("this is false");

 }
1

There are 1 best solutions below

1
underscore On

Hooks Can't be call inside function. Only in Component.

const Component = () => {
 const accountNumber = Form.useWatch("account_number", form)
}

If you want to use that write a custom hook.

const useCheckNumber = () => {

    const accountNumber = Form.useWatch("account_number", form);

    if(isNumeric(accountNumber))
        return true
    else
       return false
 }

}