-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApiAdd.js
82 lines (75 loc) · 2.28 KB
/
ApiAdd.js
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
70
71
72
73
74
75
76
77
78
79
80
81
//npm install axios
import React, { Component } from 'react';
import { axios } from 'axios';
class AddTodo extends Component {
constructor(props) {
super(props)
this.state = {
//initialize state keys value
title : "",
body : ""
}
}
onChance = (e) => {
/* Because we named the inputs to match their corresponding values in state, it's
super easy to update the state */
this.setState({[e.target.name] : e.target.value});
}
onSubmit = (e) => {
e.preventDefault() //prevent load
// get our form data out of state
const {title, body} = this.state;
// define state variable for use in return
//and store in state value
const data = { title: title, body: body};
axios.post("your api endpoint route" , data) //post your data
.then(this.setState({ //clear inputs after submit
title: "",
body: ""
}))
.then(response => this.props.history.push('/')); //redirect to route
}
render() {
const {title, body} = this.state; // define state variable for use in return
return (
<div className="container">
<div className="col-lg-9 offset-lg-1">
<form onSubmit={this.onSubmit}>
<div className="form-group">
<label>
Todo Title : </label>
<input
type="text"
name="title"
value={title}
className="form-control"
placeholder="Add Todo"
onChange={this.onChance}
required
/>
</div>
<div className="form-group">
<label>
Description : </label>
<textarea
type="text"
name="body"
className="form-control"
placeholder="Description"
value={body}
onChange={this.onChance}
rows="4"
required />
</div>
<button
type="submit"
className="btn btn-md btn-primary float-right">
Add Todo
</button>
</form>
</div>
</div>
);
}
}
export default AddTodo;