-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostForm.js
More file actions
69 lines (62 loc) · 1.4 KB
/
Copy pathPostForm.js
File metadata and controls
69 lines (62 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { Component } from "react";
import axios from "axios";
class PostForm extends Component {
constructor(props) {
super(props);
this.state = {
userId: "",
title: "",
body: ""
};
}
changeHandler = e => {
this.setState({ [e.target.name]: e.target.value });
};
submitHandler = 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>
<form onSubmit={this.submitHandler}>
<div>
<input
type="text"
name="userId"
value={userId}
onChange={this.changeHandler}
/>
</div>
<div>
<input
type="text"
name="title"
value={title}
onChange={this.changeHandler}
/>
</div>
<div>
<input
type="text"
name="body"
value={body}
onChange={this.changeHandler}
/>
</div>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
export default PostForm;