DEV Community

Cover image for Using React Hook Form with Ionic React Components

Using React Hook Form with Ionic React Components

Aaron K Saunders on March 11, 2020

see updated post example for using v6+ - https://dev.to/aaronksaunders/using-react-hook-form-with-ionic-react-components-update-1463 Setting up ...
Collapse
 
aaronksaunders profile image
Aaron K Saunders

I noticed when I ported the code, that react-hook-form in codesandbox.io is older than the latest version. In the latest version, there is a breaking change.

Before:

import { TextInput } from 'react-native';

<Controller
  as={<TextInput style={{ borderWidth: 2, borderColor: 'black'}} />}
  name="text"
  control={args => ({
    value: args[0].nativeEvent.text,
  })}
  onChange={onChange}
/>

now:

import { TextInput } from 'react-native';

<Controller
  as={<TextInput style={{ borderWidth: 2, borderColor: 'black'}} />}
  name="text"
  control={args => args[0].nativeEvent.text}
  onChange={onChange}
/>

github.com/react-hook-form/react-h...

Collapse
 
sirajulm profile image
Sirajul Muneer • Edited

@aaronksaunders Thank you. This article was a life saver. Is there a way to handle IonCheckbox ? I tried the approach similar to Radio button but, doesn't work.

Collapse
 
aaronksaunders profile image
Aaron K Saunders

just seeing this, did you get it resolved yet?

Collapse
 
sirajulm profile image
Sirajul Muneer

Not yet. I'm currently working on something else. Will update you by the end of the day.

Collapse
 
cordial_40 profile image
cordial • Edited

Thanks for the post, very useful. If I actually want to submit the data to send to an email address, can I just use something like Formspree to do so and it will work in the native iOs and Android apps? Or would it be better to put it in something like firebase and send it from there?

Collapse
 
aaronksaunders profile image
Aaron K Saunders

you can use formspree, from what I have seen it should work

Collapse
 
cordial_40 profile image
cordial

great, thanks! Will give it a go. And Thanks for replying so quickly.

Collapse
 
bluebill1049 profile image
Bill

Thank you very much for writing up this awesome blog post.

Collapse
 
aaronksaunders profile image
Aaron K Saunders

your welcome, extremely helpful hook for efficiently managing forms in react... working on a new one showing hook use in modals and overmindjs for state management

Collapse
 
bluebill1049 profile image
Bill

looking forward for that!

Collapse
 
patelrikin profile image
Rikin Patel

This is great info, not many examples are out there. I posted comment on other areas where your content is displayed for other users, but this will break if you are using react-hook-form v6+.
In order to use the method from this post, you would have to use v5.7.2 or below

Collapse
 
aaronksaunders profile image
Aaron K Saunders • Edited

working on an updated post - dev.to/ionic/using-react-hook-form...

Collapse
 
aaronksaunders profile image
Aaron K Saunders

Added a gist with the typescript version of the code to the blog post

gist.github.com/aaronksaunders/862...

Collapse
 
adrianvarelag profile image
AdrianVarelaG

Really useful, you saved me.

Collapse
 
aaronksaunders profile image
Aaron K Saunders

glad it was helpful to you!

Collapse
 
solankey016 profile image
solankey016



formState.isValid: {(formState.isValid === true).toString()}


Why is it inside a label?

Collapse
 
aaronksaunders profile image
Aaron K Saunders

not sure I understand the question

Collapse
 
malkebulan profile image
Mensah Alkebu-Lan

Great article. You may want to do an article on Formtik as well.