-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create.js
55 lines (50 loc) · 1.7 KB
/
Create.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
import { useState } from "react";
import { useNavigate } from "react-router-dom";
const Create = () => {
const [title, setTitle] = useState('');
const [body, setBody] = useState('');
const [author, setAuthor] = useState('mario');
const navigate = useNavigate();
const handleSubmit = (e) => {
e.preventDefault();
const blog = { title, body, author };
fetch('http://localhost:8000/blogs/', {
method: 'POST',
headers: { "Content-Type": "application/json" },
body: JSON.stringify(blog)
}).then(() => {
// history.go(-1);
navigate('/');
})
}
return (
<div className="create">
<h2>Add a New Blog</h2>
<form onSubmit={handleSubmit}>
<label>Blog title:</label>
<input
type="text"
required
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<label>Blog body:</label>
<textarea
required
value={body}
onChange={(e) => setBody(e.target.value)}
></textarea>
<label>Blog author:</label>
<select
value={author}
onChange={(e) => setAuthor(e.target.value)}
>
<option value="mario">mario</option>
<option value="yoshi">yoshi</option>
</select>
<button>Add Blog</button>
</form>
</div>
);
}
export default Create;