It works for multiple inputs using one function.
📌 Your State
👉 This state holds all form values in one object.
📌 The handleChange Function
Now let’s break it down 👇
🔍 Step 1: Event Trigger
When a user types in an input field:
👉 React automatically passes an event object to
handleChange
.
🔍 Step 2: event.target
This refers to the input element that was changed.
Example:
🔍 Step 3: Destructuring
This is shorthand for:
✔ Cleaner code
✔ Easier to read
🔍 Step 4: Updating State Correctly
What does this mean?
1️⃣ ...user (Spread Operator)
Copies existing values:
👉 Without this, other fields would be lost.
2️⃣ [name]: value (Dynamic Key)
This means:
OR
Based on which input triggered the event.
🎯 Final Result Example
Typing in First Name Input
State becomes:
Typing in Email Input
State becomes:
🧪 Complete Working Example
No comments:
Post a Comment
If you have any problem please let me know.