In this article, you will learn how to use React Final Form, a popular library for form management. We will cover how the final form works and see its implementation with relevant examples.
React Final Form is a lightweight Javascript form library that acts as a wrapper around a Final form which is a form state management library. React Final Form uses an observer design pattern in which the components subscribe to specific events. It does not re-render the whole form but only re-renders the subscribed fields.
React final form is a simple wrapper around a final form library. It has zero dependencies, and it is written in pure javascript. It has a modular design that is very simple to use due to its simple form of state management. It encourages us to write code for required functionality rather than unnecessary code per simple form.
Let’s set up and test react final forms functionality in our project. Set up a react project and install the final form library by running the following command.
npm install --save final-form react-final-form
After you install the library, import the main components into the file where you want to use the forms. In this example, we use the form in our main App.js file. Therefore, we import two components, Form and Field, from the react-final-form library.
The form is the main component that takes all props necessary to manage our form. The form component mainly accepts the below props:
1. Component: A react component to render for a form.
2. Debug: A callback for debugging that receives the form state and the states of all the fields. It’s called on every state change.
3. Form: If you like to construct your form
4. initialValues: Initial values to pass to your form.
5. onSubmit: a function that gets called after we submit a form.
6. Render: Accepts a component or a custom form to be rendered.
7. Validate: Returns {} or undefined when the values are valid, or an Object of validation errors when the values are invalid.
The field is a component that is used as a wrapper. In addition, it creates a Standalone final form component. The component created by the field has its state and is managed by a Form tag.
Here are some examples of the Fields props that we can use to render a Field:
1. Name: A key of your input from your initialValues object so the field and its input changes & state handling will only occur for that particular key.
2. Type: Type of input field ex: text, number, select, password.
3. Render: To render the input component.
4. Component: Optional. If you are not using ‘input,’ ‘select,’ or ‘textarea,’ it is recommended that you use
5. Validate: A function that takes the field value, all the values of the form, and the metadata about the field and returns an error if the value is invalid or undefined if the value is valid.
6. Children: You can pass your custom input component to the field via children’s props.
ex: <Field name="myField" someArbitraryOtherProp={42}> {props => { console.log(props.someArbitraryOtherProp) // would print 42 return <input {...props.input}/> }} </Field>
Let’s create a simple Email, Password form see the code below for implementation.
import React from "react"; import { render } from "react-dom"; import Styles from "./Styles"; import { Form, Field } from "react-final-form"; const onSubmit = async (values) => { console.log(values); }; const App = () => ( <Styles> <h1>React Final Form - Simple Example</h1> <Form onSubmit={onSubmit} initialValues={{ email: "", password: "" }} render={({ handleSubmit, form, submitting, pristine, values }) => ( <form onSubmit={handleSubmit}> <Field name="email"> {({ input, meta }) => ( <div> <label>Email</label> <input {...input} type="text" placeholder="Email" /> {meta.error && meta.touched && <span>{meta.error}</span>} </div> )} </Field> <Field name="password" > {({ input, meta }) => ( <div> <label>Password</label> <input {...input} type="password" placeholder="Password" /> {meta.error && meta.touched && <span>{meta.error}</span>} </div> )} </Field> <div className="buttons"> <button type="submit" disabled={submitting || pristine}> Submit </button> <button type="button" onClick={form.reset} disabled={submitting || pristine} > Reset </button> </div> </form> )} /> </Styles> ); render(<App />, document.getElementById("root"));
There are 2 types of validations provided in React Final Form.
1. Form Level Validation: This validation runs after the form is submitted
2. Field Level Validation: This validation runs when we change the field.
We cover both validations for the same email and password form in section 2.
Form Level Validation
In form-level validation, we pass the validation function into Form props and return the errors object per each field validation criteria. The error returned for a particular field then passes as a prop into a meta-object, and the user sees an error message.
import React from 'react' import { render } from 'react-dom' import Styles from './Styles' import { Form, Field } from 'react-final-form' const onSubmit = async values => { console.log(values) } const emailValid = (email)=> /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email) const passwordValid =(password) => "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,10}$".test(password) const handleValidate=(values)=>{ const errors ={} if(!emailValid(values.email)){ errors.email = "Invalid Email" } if(!passwordValid(values.password)){ errors.password = "Password must be 8 chars , at least one number one letter" } return errors; } const App = () => ( <Styles> <h1>React Final Form - Simple Example</h1> <Form onSubmit={onSubmit} initialValues={{ email: '', password :''}} validate={handleValidate} render={({ handleSubmit, form, submitting, pristine, values }) => ( <form onSubmit={handleSubmit}> <Field name="email"> {({ input, meta }) => ( <div> <label>Email</label> <input {...input} type="text" placeholder="Email" /> {meta.error && meta.touched && <span>{meta.error}</span>} </div> )} </Field> <Field name="password"> {({ input, meta }) => ( <div> <label>Password</label> <input {...input} type="password" placeholder="Password" /> {meta.error && meta.touched && <span>{meta.error}</span>} </div> )} </Field> <div className="buttons"> <button type="submit" disabled={submitting || pristine}> Submit </button> <button type="button" onClick={form.reset} disabled={submitting || pristine} > Reset </button> </div> </form> )} /> </Styles> ) render(<App />, document.getElementById('root'))
Field Level Validation
In field-level validation, we pass the validate function into Field components props and return the error message as per field validation criteria. The error returned for a particular field then passes as a prop into a meta-object, and the user sees an error message.
import React from "react"; import { render } from "react-dom"; import Styles from "./Styles"; import { Form, Field } from "react-final-form"; const onSubmit = async (values) => { console.log(values); }; const emailValid = (email) => { if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) { return "Invalid Email"; } }; const passwordValid = (password) => { if ( !"^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,10}$".test( password ) ) { return "Must contain minimum eight characters, at least one letter and one number:"; } }; const App = () => ( <Styles> <h1>React Final Form - Simple Example</h1> <Form onSubmit={onSubmit} initialValues={{ email: "", password: "" }} render={({ handleSubmit, form, submitting, pristine, values }) => ( <form onSubmit={handleSubmit}> <Field name="email" validate={emailValid}> {({ input, meta }) => ( <div> <label>Email</label> <input {...input} type="text" placeholder="Email" /> {meta.error && meta.touched && <span>{meta.error}</span>} </div> )} </Field> <Field name="password" validate={passwordValid}> {({ input, meta }) => ( <div> <label>Password</label> <input {...input} type="password" placeholder="Password" /> {meta.error && meta.touched && <span>{meta.error}</span>} </div> )} </Field> <div className="buttons"> <button type="submit" disabled={submitting || pristine}> Submit </button> <button type="button" onClick={form.reset} disabled={submitting || pristine} > Reset </button> </div> </form> )} /> </Styles> ); render(<App />, document.getElementById("root"));
In this tutorial, we learned about React final form library, how we can use Final form components to handle the form submission and validations effectively.
Contact us today to hire a react.js developer who can help you implement it in your project and take your form game to the next level!
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowMindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork