Skip to content

Commit bee3c89

Browse files
committed
Change usePrevious to sync, without any effects
1 parent 1585554 commit bee3c89

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

packages/react-querybuilder/src/hooks/useControlledOrUncontrolled.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { usePrevious } from './usePrevious';
1010
export interface UseControlledOrUncontrolledParams {
1111
defaultQuery?: RuleGroupTypeAny;
1212
queryProp?: RuleGroupTypeAny;
13-
isFirstRender: boolean;
1413
}
1514

1615
let didWarnBothQueryDefaultQuery = false;
@@ -24,9 +23,8 @@ let didWarnControlledToUncontrolled = false;
2423
export const useControlledOrUncontrolled = ({
2524
defaultQuery,
2625
queryProp,
27-
isFirstRender,
2826
}: UseControlledOrUncontrolledParams) => {
29-
const prevQueryPresent = usePrevious(!!queryProp);
27+
const prevQueryPresent = usePrevious(!!queryProp) !== false;
3028

3129
useEffect(() => {
3230
// istanbul ignore else
@@ -43,7 +41,7 @@ export const useControlledOrUncontrolled = ({
4341
console.error(errorControlledToUncontrolled);
4442
didWarnControlledToUncontrolled = true;
4543
} else if (
46-
!(prevQueryPresent || isFirstRender) &&
44+
!prevQueryPresent &&
4745
!!queryProp &&
4846
!defaultQuery &&
4947
!didWarnUncontrolledToControlled
@@ -52,5 +50,5 @@ export const useControlledOrUncontrolled = ({
5250
didWarnUncontrolledToControlled = true;
5351
}
5452
}
55-
}, [defaultQuery, prevQueryPresent, queryProp, isFirstRender]);
53+
}, [defaultQuery, prevQueryPresent, queryProp]);
5654
};
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
import { useEffect, useRef } from 'react';
1+
import { useRef } from 'react';
22

33
/**
44
* Returns the prop value from the last render.
55
*
66
* Adapted from https://usehooks.com/usePrevious/.
77
*/
88
export const usePrevious = <T>(value: T) => {
9-
const ref = useRef<T | null>(null);
9+
const ref = useRef<{ value: T | null; prev: T | null }>({
10+
value: value,
11+
prev: null,
12+
});
1013

11-
useEffect(() => {
12-
ref.current = value;
13-
}, [value]);
14+
const current = ref.current.value;
1415

15-
return ref.current;
16+
if (value !== current) {
17+
ref.current = {
18+
value: value,
19+
prev: current,
20+
};
21+
}
22+
23+
return ref.current.prev;
1624
};

packages/react-querybuilder/src/hooks/useQueryBuilderSetup.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const useQueryBuilderSetup = <
5151
type OperatorName = GetOptionIdentifierType<O>;
5252

5353
const qbId = useRef(generateID());
54-
const firstRender = useRef(true);
5554

5655
const {
5756
query: queryProp,
@@ -419,13 +418,8 @@ export const useQueryBuilderSetup = <
419418
useControlledOrUncontrolled({
420419
defaultQuery,
421420
queryProp,
422-
isFirstRender: firstRender.current,
423421
});
424422

425-
if (firstRender.current) {
426-
firstRender.current = false;
427-
}
428-
429423
return {
430424
qbId: qbId.current,
431425
rqbContext,

0 commit comments

Comments
 (0)