Skip to content

Commit 80d0ae0

Browse files
committed
use debounced effect
1 parent 61d7af8 commit 80d0ae0

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

client/src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ModalCardEdit from './Cards/ModalCardEdit'
99
import ModalLogin from './Login/ModalLogin'
1010
import DataService from './Services/DataService'
1111
import Card, { checkCardsArr } from './Cards/cardType/Card'
12+
import useDebouncedEffect from './Shared/useDebouncedEffect'
1213

1314
const { loadData, postData, updDataServLogin } = DataService()
1415

@@ -58,7 +59,7 @@ function App() {
5859
const [updaterVal] = useUpdater()
5960

6061
React.useEffect(loadDataFromServer, [logged, userName, updaterVal]) // eslint-disable-line react-hooks/exhaustive-deps
61-
React.useEffect(loadDataToServer, [cardsArr]) // eslint-disable-line react-hooks/exhaustive-deps
62+
useDebouncedEffect(loadDataToServer, [cardsArr], 1200) // eslint-disable-line react-hooks/exhaustive-deps
6263
React.useEffect(clearOldData, [logged]) // eslint-disable-line react-hooks/exhaustive-deps
6364

6465
///////////

client/src/Cards/ModalCardEdit.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import PropTypes from 'prop-types'
33
import Context from '../context'
44
import TextareaAutosize from 'react-textarea-autosize'
55
import Modal, { ModalProps } from "../Shared/Modal/Modal"
6-
import debounce from '../Shared/debounce'
76
import Card, { PropTypeCard } from './cardType/Card'
87
import Palette from './palette/palette'
98

@@ -32,7 +31,6 @@ function ModalCardEdit({ card = new Card(), index }) {
3231
modalProps.sideClose = true
3332
modalProps.onSideClick = unsetEditCard
3433

35-
3634
function open() {
3735
setShowForm(true)
3836
}
@@ -47,7 +45,7 @@ function ModalCardEdit({ card = new Card(), index }) {
4745
editCardContent(index, name, text)
4846
}
4947

50-
function onInputCange(e) {
48+
function onInputChange(e) {
5149
let name = card.name
5250
let text = card.text
5351
if (e.target.id === "modal-edit-name") name = e.target.value
@@ -82,7 +80,7 @@ function ModalCardEdit({ card = new Card(), index }) {
8280
maxRows={3}
8381
maxLength="100"
8482
value={card.name}
85-
onChange={debounce(onInputCange, 700)}
83+
onChange={onInputChange}
8684
/>
8785

8886
<p style={{ fontWeight: "500" }} className="mb-2 text-dark">
@@ -99,7 +97,7 @@ function ModalCardEdit({ card = new Card(), index }) {
9997
minRows={3}
10098
maxRows={calcMaxRows()}
10199
value={card.text}
102-
onChange={debounce(onInputCange, 1000)}
100+
onChange={onInputChange}
103101
/>
104102
</React.Fragment>
105103
) : (

client/src/Shared/debounce.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useState, useEffect } from 'react';
2+
3+
function useDebounce(value, delay) {
4+
5+
const [debouncedValue, setDebouncedValue] = useState(value);
6+
7+
useEffect(
8+
() => {
9+
10+
const handler = setTimeout(() => {
11+
setDebouncedValue(value);
12+
}, delay);
13+
14+
15+
return () => {
16+
clearTimeout(handler);
17+
};
18+
},
19+
20+
[delay, value]
21+
);
22+
23+
return debouncedValue;
24+
}
25+
26+
export default function useDebouncedEffect(func, deps, delay) {
27+
const debounced = useDebounce(deps, delay || 0)
28+
const debDepsList = Array.isArray(debounced) ? debounced : debounced ? [debounced] : undefined
29+
useEffect(func, debDepsList) // eslint-disable-line react-hooks/exhaustive-deps
30+
}

0 commit comments

Comments
 (0)