-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathindex.js
52 lines (47 loc) · 1.33 KB
/
index.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
import { useState } from "react";
import { addPhoto } from "../photos.slice";
import "./create.css";
import { useDispatch } from "react-redux";
export default function CreatePhoto() {
const [formData, setFormData] = useState({ imageUrl: "", caption: "" });
const dispatch = useDispatch();
function handleChange({ target: { name, value } }) {
setFormData({
...formData,
[name]: value,
});
}
function handleSubmit(event) {
event.preventDefault();
dispatch(addPhoto(formData));
setFormData({ imageUrl: "", caption: "" });
}
return (
<form className="create-form" onSubmit={handleSubmit}>
<h2>Add a dog</h2>
<div>
<label htmlFor="url">Enter your image's url: </label>
<input
id="url"
name="imageUrl"
onChange={handleChange}
placeholder="e.g., https://images.dog.ceo/breeds/australian-shepherd/pepper.jpg"
type="text"
value={formData.imageUrl}
/>
</div>
<div>
<label htmlFor="caption">Enter your image's caption: </label>
<input
id="caption"
name="caption"
onChange={handleChange}
placeholder="e.g., Australian Shepherd"
type="text"
value={formData.caption}
/>
</div>
<input type="submit" />
</form>
);
}