Skip to content

Commit 8f2fdbf

Browse files
author
k.golikov
committed
Fix useDebouncedMemo, improve RooksDemoPage, other fixes and changes
1 parent 77637a8 commit 8f2fdbf

File tree

7 files changed

+42
-14
lines changed

7 files changed

+42
-14
lines changed

src/hooks/debouncedMemo.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DependencyList, useEffect, useRef, useState } from 'react';
22
import { debounce, isObject, throttle } from 'lodash';
33
import { v4 } from 'uuid';
44
import call from '../utils/call';
5+
import useAutoRef from './useAutoRef';
56

67
interface NoResult {
78
_noResult: string;
@@ -11,21 +12,22 @@ const isNoResult = <T>(value: T | NoResult, noResult: NoResult): value is NoResu
1112
return isObject(value) && '_noResult' in value && value._noResult === noResult._noResult;
1213
};
1314

14-
type MemoFactory<P, R> = (params: P, noResult: NoResult) => R | NoResult;
15+
type MemoFactory<R> = (noResult: NoResult) => R | NoResult;
1516

1617
const createDebouncedMemoHook =
1718
(debounceFn: typeof debounce) =>
18-
<P, R>(params: P, factory: MemoFactory<P, R>, deps: DependencyList | undefined, wait?: number) => {
19+
<R>(factory: MemoFactory<R>, deps: DependencyList | undefined, wait?: number) => {
1920
const [value, setValue] = useState<R>();
21+
const factoryRef = useAutoRef(factory);
2022

2123
const debounceFunction = useRef(
2224
call(() => {
2325
const actualNoResult: NoResult = {
2426
_noResult: v4()
2527
};
2628

27-
return debounceFn((params: P) => {
28-
const value = factory(params, actualNoResult);
29+
return debounceFn(() => {
30+
const value = factoryRef.current(actualNoResult);
2931

3032
if (!isNoResult(value, actualNoResult)) {
3133
setValue(value);
@@ -35,7 +37,7 @@ const createDebouncedMemoHook =
3537
).current;
3638

3739
useEffect(() => {
38-
debounceFunction(params);
40+
debounceFunction();
3941
}, deps);
4042

4143
return value;

src/hooks/useAutoRef.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { MutableRefObject, useEffect, useRef } from 'react';
2+
3+
const useAutoRef = <T>(value: T): Readonly<MutableRefObject<T>> => {
4+
const valueRef = useRef<T>(value);
5+
6+
useEffect(() => {
7+
valueRef.current = value;
8+
}, [value]);
9+
10+
return valueRef;
11+
};
12+
13+
export default useAutoRef;

src/pages/bgGeneratorPage/BgGeneratorPage.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ const BgGeneratorPage = () => {
3636
const [size, setSize] = useState<Size>(getScaledScreenSize());
3737
const [color, setColor] = useState<string>('#42a5f5');
3838
const imgSource = useDebouncedMemo(
39-
{ size, color, imgCanvasRef },
40-
({ size, color, imgCanvasRef }) => {
39+
() => {
4140
if (!imgCanvasRef.current) {
4241
return;
4342
}

src/pages/htmlEditorPage/HtmlEditorPage.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ const HtmlEditorPage: FunctionComponent = () => {
118118
const handleViewModeChange = useChangeValueStateHandler(setViewMode);
119119

120120
const resultSource = useDebouncedMemo(
121-
{ sources },
122-
({ sources }) => {
121+
() => {
123122
return `
124123
<style>
125124
${sources.css}

src/pages/jsonToTypeScriptPage/JsonToTypeScriptPage.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ const JsonToTypeScriptPage = () => {
8484
}, [selectableConversionOptions]);
8585

8686
const typeScript = useDebouncedMemo(
87-
{ json, conversionOptions },
88-
({ json, conversionOptions }, noResult) => {
87+
(noResult) => {
8988
if (!json?.trim()) {
9089
setError(undefined);
9190
return '';

src/pages/rooksDemoPage/RooksDemoPage.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ const RooksDemoPage: FunctionComponent = () => {
6161
getLocalStorageKey('rooks-demo', 'localStorageState'),
6262
initialStorageState
6363
);
64+
const [localStorageState2, setLocalStorageState2] = useLocalstorageState<StorageState>(
65+
getLocalStorageKey('rooks-demo', 'localStorageState'),
66+
initialStorageState
67+
);
6468
//! useSessionstorageState
6569
const [sessionStorageState, setSessionStorageState] = useSessionstorageState<StorageState>(
6670
getLocalStorageKey('rooks-demo', 'sessionStorageState'),
@@ -93,6 +97,13 @@ const RooksDemoPage: FunctionComponent = () => {
9397
});
9498
}, []);
9599

100+
const handleLocalStorage2TextChange = useCallback<EventHandler<ChangeEvent<HTMLTextAreaElement>>>((event) => {
101+
setLocalStorageState2({
102+
...localStorageState,
103+
text: event.target.value
104+
});
105+
}, []);
106+
96107
const handleSessionStorageTextChange = useCallback<EventHandler<ChangeEvent<HTMLTextAreaElement>>>((event) => {
97108
setSessionStorageState({
98109
...sessionStorageState,
@@ -143,7 +154,10 @@ const RooksDemoPage: FunctionComponent = () => {
143154
</Col>
144155
<Col>
145156
<RooksHookHeading hooks={['useLocalstorageState']} />
146-
<TextArea value={localStorageState.text} onChange={handleLocalStorageTextChange} />
157+
<Flex column gap={6} align="stretch">
158+
<TextArea value={localStorageState.text} onChange={handleLocalStorageTextChange} />
159+
<TextArea value={localStorageState2.text} onChange={handleLocalStorage2TextChange} />
160+
</Flex>
147161
</Col>
148162
<Col>
149163
<RooksHookHeading hooks={['useSessionstorageState']} />

src/pages/rooksDemoPage/components/RooksHookHeading.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ const RooksHookHeading: FunctionComponent<Props> = ({ hooks }) => {
1313
return (
1414
<h3>
1515
<Flex row gap={8}>
16-
{hooks.map((hook) => (
17-
<ExternalLink href={getLink(hook)}>{hook}</ExternalLink>
16+
{hooks.map((hook, index) => (
17+
<ExternalLink key={index} href={getLink(hook)}>
18+
{hook}
19+
</ExternalLink>
1820
))}
1921
</Flex>
2022
</h3>

0 commit comments

Comments
 (0)