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
Typing Synthetic Events and Form Elements0%
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
9.1Typing Functional Components and Props in TypeScript9.2Typing React Hooks, Generics, and Refs9.3Typing Synthetic Events and Form Elements9.4Polymorphic Component Typing with As and ComponentPropsWithoutRef
HomeTutorialsReact.js for AdvancedTyping Synthetic Events and Form Elements
Polymorphic Component Typing with As and ComponentPropsWithoutRef

Typing Synthetic Events and Form Elements

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

React normalizes browser events into cross-browser SyntheticEvent wrappers. In TypeScript, passing inline event handlers often benefits from automatic type inference, but extracting handlers into separate named functions requires explicit typing of both the event wrapper and the underlying HTML element.


1. Common React Event Types

React exports dedicated types for every event category in the React namespace:

Event HandlerReact Event TypeTarget Element Generic
onChange (text/select)React.ChangeEvent<T>HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement
onClickReact.MouseEvent<T>HTMLButtonElement, HTMLDivElement
onSubmitReact.FormEvent<T>HTMLFormElement
onKeyDown / onKeyUpReact.KeyboardEvent<T>HTMLInputElement, HTMLElement
onFocus / onBlurReact.FocusEvent<T>HTMLInputElement
onDrag / onDropReact.DragEvent<T>HTMLDivElement

2. Typing Form Input Changes

When managing form state, pass the exact HTML form element generic to React.ChangeEvent:

1import React, { useState } from "react";
2 
3interface FormState {
4 username: string;
5 role: "developer" | "designer" | "manager";
6 bio: string;
7 newsletter: boolean;
8}
9 
10export function TypedProfileForm() {
11 const [formData, setFormData] = useState<FormState>({
12 username: "",
13 role: "developer",
14 bio: "",
15 newsletter: false,
16 });
17 
18 // Explicitly typed text input change handler
19 const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
20 const { name, value, type, checked } = e.target;
21 setFormData((prev) => ({
22 ...prev,
23 [name]: type === "checkbox" ? checked : value,
24 }));
25 };
26 
27 // Typed select change handler
28 const handleSelectChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
29 setFormData((prev) => ({
30 ...prev,
31 role: e.target.value as FormState["role"],
32 }));
33 };
34 
35 // Typed textarea change handler
36 const handleTextAreaChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
37 setFormData((prev) => ({
38 ...prev,
39 bio: e.target.value,
40 }));
41 };
42 
43 // Typed form submit handler
44 const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
45 e.preventDefault();
46 console.log("Submitting validated form payload:", formData);
47 };
48 
49 return (
50 <form onSubmit={handleSubmit} className="space-y-4 max-w-md p-6 bg-slate-900 rounded-xl text-white">
51 <div>
52 <label className="block text-xs text-slate-400 mb-1">Username</label>
53 <input
54 type="text"
55 name="username"
56 value={formData.username}
57 onChange={handleInputChange}
58 className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm focus:ring-2 focus:ring-cyan-500"
59 />
60 </div>
61 
62 <div>
63 <label className="block text-xs text-slate-400 mb-1">Role</label>
64 <select
65 value={formData.role}
66 onChange={handleSelectChange}
67 className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm"
68 >
69 <option value="developer">Developer</option>
70 <option value="designer">Designer</option>
71 <option value="manager">Manager</option>
72 </select>
73 </div>
74 
75 <div>
76 <label className="block text-xs text-slate-400 mb-1">Bio</label>
77 <textarea
78 value={formData.bio}
79 onChange={handleTextAreaChange}
80 className="w-full px-3 py-2 bg-slate-800 border border-slate-700 rounded text-sm h-20"
81 />
82 </div>
83 
84 <button type="submit" className="w-full py-2 bg-cyan-600 hover:bg-cyan-500 font-semibold rounded text-sm">
85 Save Profile
86 </button>
87 </form>
88 );
89}

3. Keyboard Event Handling & Key Filtering

1const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
2 if (e.key === "Enter") {
3 e.preventDefault();
4 handleSearch();
5 } else if (e.key === "Escape") {
6 clearSearch();
7 }
8};

4. e.target vs e.currentTarget

TypeScript differentiates between e.target and e.currentTarget:

  • e.currentTarget: The element to which the event handler is currently attached (strictly typed to generic T).
  • e.target: The element that dispatched the original event (could be a nested child span or icon, typed as EventTarget).

Always use e.currentTarget when reading properties on the registered element container!


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the correct type signature for an onChange handler bound to an <input type="text">?
Question 2
What is the difference between e.target and e.currentTarget in a typed React mouse event?
Question 3
Why is e.preventDefault() required on form onSubmit events?
Question 4
Which React event type should be used when handling drag and drop interactions?
Question 5
How can you check if the user pressed the Enter key in a type-safe manner?
Next Lesson

Polymorphic Component Typing with As and ComponentPropsWithoutRef

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

Related Lessons

Previous LessonNext Lesson
Typing React Hooks, Generics, and RefsPolymorphic Component Typing with As and ComponentPropsWithoutRef

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