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

Advanced: Form state management step 2 – useLoginForm #11

Draft
wants to merge 6 commits into
base: lessons/08-react-apis
Choose a base branch
from

Conversation

lucasconstantino
Copy link
Contributor

@lucasconstantino lucasconstantino commented Jun 13, 2022

⚠️ Warning

The changes contained in this PR are considered advanced, and won't be part of the final application. We'll leave it here as a reference and for learning purposes.


With the introduction of login form state management after Lesson 08: Built in React APIs, we've learned how hard it can become to handle form state manually – input values, validation states, errors, submit state, etc. And, if we manage to have the state itself working, chances are the UX won't be the best anyway. That's probably enough for us to realize how form libraries are essential to building high-quality forms in our applications.

But, what about we push a little bit more, and pretend there was no form library at our disposal? Turns out this is a great opportunity for us to play around with React APIs.

This PR builds on top of the the previous login form state managing to cover:

  1. Abstract away form submitting logic and state (code) – let your handlers care for the application needs only (code)
  2. Abstract away low-level event handling (code, code, and code)
  3. Touch states
    1. Mark inputs as touched when they stop interacting with an input (code)
    2. Bother users only after they have interacted with the input (code)
    3. Or after as soon as they try to submit the form (code)

@vercel
Copy link

vercel bot commented Jun 13, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
frontend-academy-2022 ✅ Ready (Inspect) Visit Preview Jun 13, 2022 at 5:35PM (UTC)

@lucasconstantino lucasconstantino changed the title Advanced: Form state management step 2 Advanced: Form state management step 2 – useLoginForm Jun 13, 2022
@lucasconstantino lucasconstantino marked this pull request as draft June 13, 2022 17:39
setTouched((touched) => ({ ...touched, [name]: true }))
}, [])

return {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not gonna pretend I understand all the type syntax used, but it's nice to what potentially Formik or ReactHooks are doing under the hood.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely agree. Until this point I was pretty confident whats going on in our project, but this one seems more like an expert approach. Nice showcase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OndrejDrapalik @matyasmihalka this definitely isn't the most straight-forward code. In fact, this is why we decided it wasn't necessary to include as our "main track" in the application.

The idea here is really to showcase how libraries such as Formik are made, and how complex proper form state handling can be without those libraries.

I'll give you a hint though: React Hook Forms is way, waaay more complex than Formik, as it takes over React entirely, building it's solution on top of traps and Proxy states, executing hooks dynamically based on state consumption. Quite neat. If you are interested, we could have a chat about it or explore a simpler isolated solution to showcase the technique.

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

Successfully merging this pull request may close these issues.

3 participants