Skip to content

Commit

Permalink
⬆️ HTTP Post Request
Browse files Browse the repository at this point in the history
HTTP Post Request
  • Loading branch information
SaishJ committed Sep 13, 2022
1 parent c33bcb1 commit ebca682
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,5 @@ export default UpdatedComponent;
### [Render Peops](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/d60d9bec7747e75247a97d22406520cbac48fa0a)

### [Context](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/c925fec6db26889ac391d1c8cd072b67e40c2b49)

### [HTTP Get Request](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/c33bcb13cc37673c610879674e3a1190e574963e)
4 changes: 3 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import HoverCounterTwo from "./components/HoverCounterTwo";
import { UserProvider } from "./components/useContext";
import ComponentA from "./components/ComponentA";
import PostList from "./components/PostList";
import PostForm from "./components/PostForm";

function App() {
return (
<div className="App">
<PostList />
<PostForm />
{/* <PostList /> */}
{/* <UserProvider value="Saish">
<ComponentA />
</UserProvider> */}
Expand Down
72 changes: 72 additions & 0 deletions src/components/PostForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { Component } from "react";
import axios from "axios";

export class PostForm extends Component {
constructor(props) {
super(props);

this.state = {
userId: "",
title: "",
body: "",
};
}

changeHandler = (e) => {
this.setState({ [e.target.name]: e.target.value });
};

handleSubmit = (e) => {
e.preventDefault();
console.log(this.state);
axios
.post("https://jsonplaceholder.typicode.com/posts", this.state)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
});
};

render() {
const { userId, title, body } = this.state;
return (
<div>
<h1>User Form</h1>
<form onSubmit={this.handleSubmit}>
<div>
<input
type="text"
placeholder="Id"
name="userId"
value={userId}
onChange={this.changeHandler}
/>
</div>
<div>
<input
type="text"
placeholder="Title"
name="title"
value={title}
onChange={this.changeHandler}
/>
</div>
<div>
<input
type="text"
placeholder="Comment"
name="body"
value={body}
onChange={this.changeHandler}
/>
</div>
<button type="submit">Submit</button>
</form>
</div>
);
}
}

export default PostForm;

0 comments on commit ebca682

Please sign in to comment.