-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
49 lines (44 loc) · 1.27 KB
/
App.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
import React, { useState } from 'react';
import './App.css';
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YR9EHTUZLF', '0fa905cc79c37865c895cbd920b33420');
const index = client.initIndex('test_firstSearchIndex');
function App() {
const [searchKey, setSearchKey] = useState("");
const [show, setShow] = useState(false);
const [results, setResults] = useState();
function search() {
if (searchKey === "") { return; }
index.search({ 'query': searchKey })
.then(({ hits } = {}) => {
setResults(hits);
}).catch(err => {
console.log(err);
console.log(err.debugData);
});
}
return (
<div className="App">
<p>
Algolia Search with firstname
</p>
<input type="text"
placeholder="Search"
onKeyPress={(event) => {
if (event.key === 'Enter') {
search(event);
setShow(true);
} else {
setShow(false);
}
}}
onChange={(event) => setSearchKey(event.target.value)} />
{show && results !== undefined &&
results.map((value, key) => {
return <p key={key}>Firstname : {value.firstname} Lastname : {value.lastname}</p>
})
}
</div>
);
}
export default App;