-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.js
53 lines (50 loc) · 1.4 KB
/
Home.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
import { useState } from "react";
import Axios from "axios";
import Searchof from "../Components/Searchof";
function Home() {
const [input1Value, setInput1Value] = useState("");
const [searchResults, setSearchResults] = useState([]);
const [searchNot, setSearchNot] = useState(false);
function searchClicked() {
let searchURL = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=";
Axios.get(searchURL + input1Value)
.then((item) => {
console.log(item.data.drinks);
if (item.data.drinks == null) {
setSearchResults([]);
setSearchNot(true);
} else {
setSearchNot(false);
setSearchResults(item.data.drinks);
}
})
.catch((error) => {
console.log(error);
});
}
return (
<div>
<h3>Search Coctails</h3>
<input
onChange={(e) => setInput1Value(e.target.value)}
value={input1Value}
type="text"
name=""
id=""
/>
<button className="button1" onClick={searchClicked}>
Search
</button>
<br />
{searchNot && <h1>Search term not found !</h1>}
{searchResults.map((data, index) => (
<Searchof
key={index}
title={data.strDrink}
image={data.strDrinkThumb}
></Searchof>
))}
</div>
);
}
export default Home;