Skip to content

Instantly share code, notes, and snippets.

@patwadeepak
Created July 23, 2021 03:46
Show Gist options
  • Save patwadeepak/818a043355922c274dddfbab7e42f969 to your computer and use it in GitHub Desktop.
Save patwadeepak/818a043355922c274dddfbab7e42f969 to your computer and use it in GitHub Desktop.
Pure React Way - PostForm.js
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