Skip to content

Commit ad89003

Browse files
committed
debouncing
1 parent de5524c commit ad89003

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react';
22

33
const Debouncing = () => {
4+
const [inputValue, setInputValue] = useState('');
5+
const [debouncedValue, setDebouncedValue] = useState('');
6+
7+
useEffect(() => {
8+
const handler = setTimeout(() => {
9+
setDebouncedValue(inputValue);
10+
}, 300);
11+
return () => {
12+
clearTimeout(handler);
13+
};
14+
}, [inputValue]);
15+
16+
const handleChange = (event) => {
17+
setInputValue(event.target.value);
18+
};
19+
420
return (
5-
<div>
6-
<div>sameer</div>
7-
21+
<div className='flex flex-col h-screen bg-gray-800/20 items-center justify-center'>
22+
<input
23+
className='bg-transparent border-2 border-gray-500 rounded-lg w-1/2 px-10 py-3'
24+
type="text"
25+
value={inputValue}
26+
onChange={handleChange}
27+
placeholder="Type something..."
28+
/>
29+
<p>Input value: {inputValue}</p>
30+
<p>Debounced value: {debouncedValue}</p>
831
</div>
9-
)
10-
}
32+
);
33+
};
1134

12-
export default Debouncing
35+
export default Debouncing;

0 commit comments

Comments
 (0)