Slide 1

Slide 1 text

Let’s simplify your complex forms! @kenza_iraki

Slide 2

Slide 2 text

Intro • Hello, I’m Kenza !" • Curator of Let’s React (a React Native newsletter) • Founder of React MTL • Software Engineer @ Breathe Life • Hedgehog mommy

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

So what’s the matter with forms? • Boilerplate • Validation ✅ • Error handling ❌ • Submission

Slide 5

Slide 5 text

Case Study: The “simple” sign up form

Slide 6

Slide 6 text

The “simple” sign up form • 4 fields (email, password, name and phone number) • Validation on all fields • 2 api calls (sign up and then login) • Error handling

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

New requirements • Toggle between display/hide your password • Confirm password field • Real time display of password validation to the user

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

The more complex your form, the messier it gets • Optional vs required fields • Fields that depend on answers to other fields • Hide/display fields • Change default values • Make fields disabled

Slide 13

Slide 13 text

OK, fine, forms can get pretty crazy. So what can I do about it?

Slide 14

Slide 14 text

Option #1: Redux Form Redux-based solution to forms, which keeps everything form-related in your Redux state (fields’ values, currently focused field, previously focused fields, sync and async validation, errors, submission status etc.)

Slide 15

Slide 15 text

Option #1: Redux Form Pros • Single source of truth • Easily share form data between components • Since it uses Redux, you get great debugging tools out of the box • Very widely used (390k downloads / week)

Slide 16

Slide 16 text

Option #1: Redux Form Cons • Only makes sense if you’re already using Redux • Redux store is updated on every key stroke • Unnecessary and costly as your app grows • React prophet and Redux creator Dan Abramov said that “Form state is inherently ephemeral, so tracking it in Redux is unnecessary” • Business logic can get very complicated • Large bundle size (~ 26.7 kB minified + gzipped)

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Option #2: Final Form Subscription based form state management library that uses the observer pattern, built by the author of Redux Form

Slide 19

Slide 19 text

Option #2: React Final Form Pros • Framework agnostic (can be used with React, Vue, Angular, vanilla JS…) • Zero configuration • Very small bundle (~ 4.6 kB minified + gzipped)

Slide 20

Slide 20 text

Option #2: React Final Form Cons • A lot of “magic” going on (I don’t like the Observer Pattern) • Hard to debug • The least widely used option (95k downloads / week)

Slide 21

Slide 21 text

Option #2: React Final Form

Slide 22

Slide 22 text

Option #3: Formik Form helper library that handles the 3 core things you need: • Getting values in and out of form state • Validation and error messages • Handling form submission

Slide 23

Slide 23 text

Option #3: Formik Pros •Zero configuration •Powerful validation with yup out of the box •Support for nested structures (e.g. address.city) •No magic - just a plain old React component •Relatively small bundle (~ 11.9 kB minified + gzipped) •Very widely used (500k downloads / week) •Recommended in the official React docs

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Yup

Slide 26

Slide 26 text

Yup

Slide 27

Slide 27 text

Option #3: Formik Cons • Framework-specific (React/Native only) • Inflexible in some ways • Claims to only support 80% of use cases, so when getting to the last 20%, things get weird • On long forms using styled components and/or Material UI, there is a significant performance issue • The entire form validates on every key press

Slide 28

Slide 28 text

Conclusion • Redux Form is probably not a good idea for React Native apps • Final Form is okay if you’re fine with some magic • Formik is pretty good, but not one-size-fits-all

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Disclaimer ⚠ No animals were harmed in the context of this presentation @kenza_iraki