Created
July 23, 2021 03:46
-
-
Save patwadeepak/818a043355922c274dddfbab7e42f969 to your computer and use it in GitHub Desktop.
Pure React Way - PostForm.js
This file contains 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 { useState } from "react"; | |
const PostForm = ({ addPost }) => { | |
const [formData, setFormData] = useState({ | |
title: "", | |
body: "", | |
}); | |
const handleChange = (ev) => { | |
setFormData({ | |
...formData, | |
[ev.target.name]: ev.target.value, | |
}); | |
}; | |
const handlePostIt = (ev) => { | |
ev.preventDefault(); | |
addPost(formData); | |
setFormData({ title: "", body: "" }); | |
}; | |
return ( | |
<div className="postform-container"> | |
<label htmlFor="title">Title</label> | |
<input | |
type="text" | |
name="title" | |
onChange={handleChange} | |
value={formData.title} | |
/> | |
<br /> | |
<label htmlFor="body">Post</label> | |
<textarea name="body" onChange={handleChange} value={formData.body} /> | |
<br /> | |
<button type="submit" onClick={handlePostIt}> | |
Post it | |
</button> | |
</div> | |
); | |
}; | |
export default PostForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment