-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebouncing.jsx
More file actions
88 lines (73 loc) · 1.54 KB
/
Debouncing.jsx
File metadata and controls
88 lines (73 loc) · 1.54 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, { useEffect, useState } from 'react';
import { View, Text, TextInput, StyleSheet } from 'react-native';
function App() {
const [word, setWord] = useState('');
const [debounceInput, setDebounceInput] = useState('');
const [searchResult, setSearchResult] = useState([]);
const data = [
'app',
'apple',
'bed',
'bedminton',
'face',
'facebook'
];
useEffect(() => {
let timer = setTimeout(() => {
if(word.trim()) {
setDebounceInput(word);
}
else {
setSearchResult([]);
}
}, 2000);
return () => clearTimeout(timer);
}, [word]);
useEffect(() => {
if(debounceInput) {
handleSearch(debounceInput);
}
}, [debounceInput]);
const handleSearch = (value) => {
let result = data.filter((item) =>
item.toLowerCase().includes(value.toLowerCase())
)
setSearchResult(result);
}
return (
<View style={styles.container}>
<TextInput
value={word}
onChangeText={setWord}
style={styles.input}
placeholder='search ...'
/>
<View>
{
searchResult.map((item, index) => {
return (
<Text>{item}</Text>
)
})
}
</View>
</View>
)
}
const styles = StyleSheet.create({
input: {
width: '90%',
height: 30,
borderColor: 'blue',
borderWidth: 2,
borderRadius: 6,
marginTop: 20,
padding: 6,
},
container: {
flex: 1,
alignItems: 'center',
backgroundColor: 'pink'
}
})
export default App;