-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApiEdit.js
78 lines (69 loc) · 3.04 KB
/
ApiEdit.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
//npm install axios
import React, { Component } from "react";
import { axios } from 'axios';
class APIEdit extends Component {
constructor(props) {
super(props);
this.state = {
title :'',
body: ''
};
}
componentDidMount() {
const id = "your id like 1245";
axios.get("your api endpoint route", id)
.then(response => this.setState({
title: response.data.title,
body: response.data.body
}));
}
onChance = (e) => {
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
data = { title: title, body: body }
axios.put("your api endpoint route", data) // url and update data pass through put axios method
.then(response => this.props.history.push('/'));
}
render() {
const { title, body } = this.state;
return (
<div className="container">
<div className="col-lg-6 offset-2 mt-5">
<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}
required rows="6" />
</div>
<button
type="submit"
className="btn btn-lg btn-primary float-right mt-2">
Update
</button>
</form>
</div>
</div>
);
}
}
export default APIEdit;