informed icon indicating copy to clipboard operation
informed copied to clipboard

Getting errors in form api hooks

Open victorbf opened this issue 6 years ago • 5 comments

I'm getting this error every time i try to use a function in formApi hooks Uncaught TypeError: formApi.setValue is not a function

Here is what i'm getting by consoling logging it: Screen Shot 2019-12-19 at 18 22 14

Here is my code: Screen Shot 2019-12-19 at 18 29 01

I'm using version 2.3.0 but i already tried to upgrade it.

victorbf avatar Dec 19 '19 21:12 victorbf

You can try this insteand of useFormApi:

<Form> { ({ formState, formApi }) => ( <> <Field field="fieldName"/> <button onClick={ev => { formApi .setValue('fieldName', 'test'); // or // formApi .setValues({ 'fieldName': 'test' }); }} > click button </button> </> ) }

CRGavrila avatar Dec 20 '19 08:12 CRGavrila

Thanks that work it! But i solved as well, hooks works only in the children component not in the one who is calling the FORM component. Sorry about that.

victorbf avatar Dec 20 '19 12:12 victorbf

^^^^ @victorbf yes you cant use hooks outside of the <Form />

joepuzzo avatar Dec 20 '19 14:12 joepuzzo

Got stuck on this too. Maybe the docs for useFormApi and useFieldApi aren't the best examples because it expects forms are broken into multiple sections which are components instead of one big chunk of JSX.

Almost all our forms (so far) are fully contained in one-file/one-component for simplicity (ex: contactForm.js). I wouldn't have figured it out without the comment from @CRGavrila.

Re-posting that code with formatting for future reference.

<Form>
    {({ formApi }) => (
        <Fragment>
            <Field field="fieldName"/>

            <button onClick={() => formApi.setValue('fieldName', 'go bananas')} >
                click button
            </button>
        </Fragment>
    )}
</Form>

brendanfalkowski avatar Apr 11 '20 00:04 brendanfalkowski

This ^^^ is in the docs on the first page

joepuzzo avatar Apr 11 '20 20:04 joepuzzo