Created
November 19, 2022 12:00
-
-
Save Tribhuwan-Joshi/0c18af845de62d1ebe0e5695f764db7c to your computer and use it in GitHub Desktop.
React form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from "react" | |
| export default function App() { | |
| const [formData , setFormData] = React.useState({ | |
| email:"", | |
| password:"", | |
| confirmation:"", | |
| newsletter:false | |
| }); | |
| function handleChange(event){ | |
| const {name , value , checked ,type } = event.target; | |
| setFormData(prev=>{ | |
| return {...prev , | |
| [name] : type === "checkbox" ? checked: value | |
| } | |
| }); | |
| console.log(formData) | |
| } | |
| function handleSubmit(event) { | |
| event.preventDefault() | |
| if(formData.newsletter) console.log("Thanks for signing up for our newsletter!") | |
| if(formData.password === formData.confirmation) console.log("Sign up") | |
| else console.log("Password do not match") | |
| } | |
| return ( | |
| <div className="form-container"> | |
| <form className="form" onSubmit={handleSubmit}> | |
| <input | |
| type="email" | |
| placeholder="Email address" | |
| className="form--input" | |
| name="email" | |
| onChange={handleChange} | |
| /> | |
| <input | |
| type="password" | |
| placeholder="Password" | |
| className="form--input" | |
| name="password" | |
| onChange={handleChange} | |
| /> | |
| <input | |
| type="password" | |
| placeholder="Confirm password" | |
| className="form--input" | |
| name="confirmation" | |
| onChange={handleChange} | |
| /> | |
| <div className="form--marketing"> | |
| <input | |
| id="okayToEmail" | |
| type="checkbox" | |
| name="newsletter" | |
| onChange={handleChange} | |
| /> | |
| <label htmlFor="okayToEmail">I want to join the newsletter</label> | |
| </div> | |
| <button | |
| className="form--submit" | |
| > | |
| Sign up | |
| </button> | |
| </form> | |
| </div> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment