Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting errors in form api hooks #261

Closed
victorbf opened this issue Dec 19, 2019 · 5 comments
Closed

Getting errors in form api hooks #261

victorbf opened this issue Dec 19, 2019 · 5 comments

Comments

@victorbf
Copy link

victorbf commented Dec 19, 2019

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.

@CRGavrila
Copy link

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> </> ) }

@victorbf
Copy link
Author

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.

@joepuzzo
Copy link
Collaborator

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

@brendanfalkowski
Copy link

brendanfalkowski commented Apr 11, 2020

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>

@joepuzzo
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants