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 3 – useForm #12

Draft
wants to merge 2 commits into
base: lessons/08-react-apis--part-2
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.


We've covered how to go further on UX and state managing by adding touch states and abstracting form state managing to a separate hook. Now, what about we make this whole logic reusable to other forms?

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

  1. Extract form state management from useLoginForm to useForm
  2. Make form state generic, so it can be used with different form inputs (code)
  3. Update useLoginForm to use useForm (code)

That's it! Now we could potentially use the same useForm hook to other forms with have :)

@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:38PM (UTC)

@lucasconstantino lucasconstantino marked this pull request as draft June 13, 2022 17:39
/**
* Controls login form state.
*/
const useForm = <TFormData extends object>(

Choose a reason for hiding this comment

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

I was just curious, if i use Record<string, any> instead of object, that would lead to stronger typing right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, I would say. object is more strict than Record<string, any>. We want the TFormData to be either provided or inferable from the initialValues. The object here helps with creating awareness when I cannot infer nor have the type passed in:

With TFormData extends object (warns):
Screen Shot 2022-06-16 at 17 21 05

With TFormData extends Record<string, any> (does not warn):
Screen Shot 2022-06-16 at 17 21 45

Choose a reason for hiding this comment

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

Aaah, i see. Thank you, that make sense 💯 I saw the one with the Record in some example, now i see the difference.

@lucasconstantino lucasconstantino changed the base branch from main to lessons/08-react-apis--part-2 August 12, 2022 13:14
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.

2 participants