MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Formik Architecture and Field Arrays0%
Study Materials
React.js for AdvancedProgress 0%
1.1Control Props and Inversion of Control Pattern1.2State Reducer Pattern in Advanced React1.3Prop Getters and Flexible Design Systems1.4Headless UI Architecture and Accessibility Primitives
12.1Formik Architecture and Field Arrays12.2Multi-Step Wizards and Dynamic Schema Validation with Yup12.3Custom Form Controls with useField12.4High-Performance Form Optimization Comparison
HomeTutorialsReact.js for AdvancedFormik Architecture and Field Arrays
Multi-Step Wizards and Dynamic Schema Validation with Yup

Formik Architecture and Field Arrays

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Handling complex enterprise forms—with dynamic nested objects, repetitive rows, conditional validations, and asynchronous submission states—requires a robust form orchestration library. Formik manages form state, validation, error messages, and touched states. Its FieldArray component is the standard for managing dynamic repeatable lists (such as invoice line items, invite lists, or dynamic configuration rules).


1. Formik Core Architecture

Formik separates form state into four essential lifecycle models:

  • values: The current JavaScript object representing all input values.
  • errors: An object mirroring the keys of values containing string validation error messages.
  • touched: A boolean map tracking which inputs the user has physically interacted with and blurred, preventing premature error display.
  • isSubmitting: A boolean flag tracking asynchronous submission progress.

2. Dynamic Line Items with <FieldArray>

In applications like invoicing or procurement systems, users need to dynamically add, remove, and reorder line items. Formik's FieldArray provides helper methods (push, remove, swap, insert) while keeping validation errors in sync.

1import React from "react";
2import { Formik, Form, Field, FieldArray, ErrorMessage } from "formik";
3import * as Yup from "yup";
4 
5interface LineItem {
6 description: string;
7 quantity: number;
8 unitPrice: number;
9}
10 
11interface InvoiceFormValues {
12 clientName: string;
13 invoiceDate: string;
14 items: LineItem[];
15}
16 
17const InvoiceSchema = Yup.object().shape({
18 clientName: Yup.string().required("Client name is required"),
19 invoiceDate: Yup.date().required("Invoice date is required"),
20 items: Yup.array()
21 .of(
22 Yup.object().shape({
23 description: Yup.string().required("Description required"),
24 quantity: Yup.number().min(1, "Min qty is 1").required("Required"),
25 unitPrice: Yup.number().min(0.01, "Min price is $0.01").required("Required"),
26 })
27 )
28 .min(1, "You must specify at least one line item"),
29});
30 
31const initialValues: InvoiceFormValues = {
32 clientName: "",
33 invoiceDate: new Date().toISOString().split("T")[0],
34 items: [{ description: "Cloud Infrastructure Setup", quantity: 1, unitPrice: 1200 }],
35};
36 
37export function InvoiceBuilder() {
38 return (
39 <div className="max-w-3xl p-8 bg-slate-900 text-white rounded-xl">
40 <h2 className="text-xl font-bold mb-6">Create Enterprise Invoice</h2>
41 
42 <Formik
43 initialValues={initialValues}
44 validationSchema={InvoiceSchema}
45 onSubmit={async (values, { setSubmitting }) => {
46 console.log("Submitting Invoice:", values);
47 await new Promise((r) => setTimeout(r, 1000));
48 setSubmitting(false);
49 }}
50 >
51 {({ values, isSubmitting }) => (
52 <Form className="space-y-6">
53 <div className="grid grid-cols-2 gap-4">
54 <div>
55 <label className="block text-xs text-slate-400 mb-1">Client Name</label>
56 <Field
57 name="clientName"
58 className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm text-white"
59 />
60 <ErrorMessage name="clientName" component="div" className="text-rose-400 text-xs mt-1" />
61 </div>
62 
63 <div>
64 <label className="block text-xs text-slate-400 mb-1">Date</label>
65 <Field
66 name="invoiceDate"
67 type="date"
68 className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm text-white"
69 />
70 <ErrorMessage name="invoiceDate" component="div" className="text-rose-400 text-xs mt-1" />
71 </div>
72 </div>
73 
74 {/* Dynamic Repeatable Field Array */}
75 <div className="border-t border-slate-800 pt-4">
76 <h3 className="text-sm font-semibold text-cyan-400 mb-3">Invoice Line Items</h3>
77 
78 <FieldArray name="items">
79 {({ push, remove }) => (
80 <div className="space-y-3">
81 {values.items.map((item, index) => (
82 <div key={index} className="flex gap-3 items-start bg-slate-950 p-3 rounded-lg border border-slate-800">
83 <div className="flex-1">
84 <Field
85 name={`items.${index}.description`}
86 placeholder="Item description"
87 className="w-full px-3 py-1.5 bg-slate-800 border border-slate-700 rounded text-xs text-white"
88 />
89 <ErrorMessage
90 name={`items.${index}.description`}
91 component="div"
92 className="text-rose-400 text-xs mt-0.5"
93 />
94 </div>
95 
96 <div className="w-24">
97 <Field
98 name={`items.${index}.quantity`}
99 type="number"
100 placeholder="Qty"
101 className="w-full px-3 py-1.5 bg-slate-800 border border-slate-700 rounded text-xs text-white"
102 />
103 <ErrorMessage
104 name={`items.${index}.quantity`}
105 component="div"
106 className="text-rose-400 text-xs mt-0.5"
107 />
108 </div>
109 
110 <div className="w-28">
111 <Field
112 name={`items.${index}.unitPrice`}
113 type="number"
114 placeholder="Price"
115 className="w-full px-3 py-1.5 bg-slate-800 border border-slate-700 rounded text-xs text-white"
116 />
117 <ErrorMessage
118 name={`items.${index}.unitPrice`}
119 component="div"
120 className="text-rose-400 text-xs mt-0.5"
121 />
122 </div>
123 
124 {values.items.length > 1 && (
125 <button
126 type="button"
127 onClick={() => remove(index)}
128 className="px-2 py-1.5 bg-rose-900/40 text-rose-300 hover:bg-rose-900 rounded text-xs"
129 >
130 Remove
131 </button>
132 )}
133 </div>
134 ))}
135 
136 <button
137 type="button"
138 onClick={() => push({ description: "", quantity: 1, unitPrice: 0 })}
139 className="px-3 py-1.5 bg-slate-800 hover:bg-slate-700 text-cyan-400 text-xs font-semibold rounded border border-slate-700"
140 >
141 + Add Item Row
142 </button>
143 </div>
144 )}
145 </FieldArray>
146 </div>
147 
148 <button
149 type="submit"
150 disabled={isSubmitting}
151 className="w-full py-2.5 bg-cyan-600 hover:bg-cyan-500 font-semibold rounded text-sm disabled:opacity-50"
152 >
153 {isSubmitting ? "Generating Invoice..." : "Submit Invoice"}
154 </button>
155 </Form>
156 )}
157 </Formik>
158 </div>
159 );
160}

3. Touched State and Validation Timing

Displaying errors when the form first mounts overwhelms users with red text before they have typed anything. Formik tracks field touched state via handleBlur:

1// Only show error message if the user has focused and blurred away from the field
2{touched.clientName && errors.clientName && (
3 <span className="text-xs text-rose-400">{errors.clientName}</span>
4)}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary purpose of Formik's FieldArray component?
Question 2
Why does Formik maintain a touched state alongside errors and values?
Question 3
In Yup validation schemas, how do you validate an array of objects inside FieldArray?
Question 4
What does the isSubmitting boolean prop in Formik indicate?
Question 5
How do nested field names like name="items.0.description" function in Formik?
Next Lesson

Multi-Step Wizards and Dynamic Schema Validation with Yup

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Visual Regression and Snapshot Testing StrategiesMulti-Step Wizards and Dynamic Schema Validation with Yup

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext