-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.js
42 lines (35 loc) · 882 Bytes
/
Search.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
import React, { useState, useCallback } from 'react'
const Search = () => {
const [value, setValue] = useState('')
const handleChange = useCallback(
(e) => {
setValue(e.target.value);
},
[]
)
const handleClick = useCallback(
async () => {
const fetchData = async () => {
const url = `https://sdamgia-homework-backend.herokuapp.com/api/search?query=${value}`
const response = await fetch(url)
const data = await response.json()
return data.subject
}
const { name, title } = await fetchData()
alert(`${name} - ${title}`)
},
[value]
)
return (
<div>
<input
type="text"
placeholder="Searching..."
value={value}
onChange={handleChange}
/>
<button onClick={handleClick}>Поиск</button>
</div>
)
}
export default Search