Skip to content

Commit 8c29557

Browse files
Tom910facebook-github-bot
authored andcommitted
simplify dependencies for loadMore function
Reviewed By: tyao1 Differential Revision: D66556691 fbshipit-source-id: a058fe4aa6a9630fdb00793625748a27c57e2278
1 parent a8a0750 commit 8c29557

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

packages/react-relay/relay-hooks/useLoadMoreFunction.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ hook useLoadMoreFunction_CURRENT<TVariables: Variables>(
138138
};
139139
}, [disposeFetch]);
140140

141+
const isRequestInvalid = fragmentData == null || isParentQueryActive;
142+
141143
const loadMore = useCallback(
142144
(
143145
count: number,
@@ -166,11 +168,13 @@ hook useLoadMoreFunction_CURRENT<TVariables: Variables>(
166168
}
167169

168170
const fragmentSelector = getSelector(fragmentNode, fragmentRef);
169-
if (
170-
isFetchingRef.current === true ||
171-
fragmentData == null ||
172-
isParentQueryActive
173-
) {
171+
172+
const isRequestInvalidCheck =
173+
RelayFeatureFlags.OPTIMIZE_RECREATING_LOAD_MORE_FUNCTION
174+
? isRequestInvalid
175+
: fragmentData == null || isParentQueryActive;
176+
177+
if (isFetchingRef.current === true || isRequestInvalidCheck) {
174178
if (fragmentSelector == null) {
175179
warning(
176180
false,
@@ -271,8 +275,10 @@ hook useLoadMoreFunction_CURRENT<TVariables: Variables>(
271275
disposeFetch,
272276
completeFetch,
273277
isFetchingRef,
274-
isParentQueryActive,
275-
fragmentData,
278+
isRequestInvalid,
279+
...(RelayFeatureFlags.OPTIMIZE_RECREATING_LOAD_MORE_FUNCTION
280+
? [isRequestInvalid]
281+
: [isParentQueryActive, fragmentData]),
276282
fragmentNode.name,
277283
fragmentRef,
278284
componentDisplayName,

packages/react-relay/relay-hooks/useLoadMoreFunction_EXPERIMENTAL.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const invariant = require('invariant');
3131
const {useCallback, useRef, useState} = require('react');
3232
const {
3333
__internal: {fetchQuery},
34+
RelayFeatureFlags,
3435
createOperationDescriptor,
3536
getPaginationVariables,
3637
getRefetchMetadata,
@@ -135,6 +136,8 @@ hook useLoadMoreFunction_EXPERIMENTAL<TVariables: Variables>(
135136
connectionPathInFragmentData,
136137
);
137138

139+
const isRequestInvalid = fragmentData == null || isParentQueryActive;
140+
138141
const isMountedRef = useIsMountedRef();
139142
const loadMore = useCallback(
140143
(
@@ -164,11 +167,13 @@ hook useLoadMoreFunction_EXPERIMENTAL<TVariables: Variables>(
164167
}
165168

166169
const fragmentSelector = getSelector(fragmentNode, fragmentRef);
167-
if (
168-
fetchStatusRef.current.kind === 'fetching' ||
169-
fragmentData == null ||
170-
isParentQueryActive
171-
) {
170+
171+
const isRequestInvalidCheck =
172+
RelayFeatureFlags.OPTIMIZE_RECREATING_LOAD_MORE_FUNCTION
173+
? isRequestInvalid
174+
: fragmentData == null || isParentQueryActive;
175+
176+
if (fetchStatusRef.current.kind === 'fetching' || isRequestInvalidCheck) {
172177
if (fragmentSelector == null) {
173178
warning(
174179
false,
@@ -267,8 +272,9 @@ hook useLoadMoreFunction_EXPERIMENTAL<TVariables: Variables>(
267272
identifierValue,
268273
direction,
269274
cursor,
270-
isParentQueryActive,
271-
fragmentData,
275+
...(RelayFeatureFlags.OPTIMIZE_RECREATING_LOAD_MORE_FUNCTION
276+
? [isRequestInvalid]
277+
: [isParentQueryActive, fragmentData]),
272278
fragmentNode.name,
273279
fragmentRef,
274280
componentDisplayName,

packages/relay-runtime/util/RelayFeatureFlags.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ export type FeatureFlags = {
5555
// Temporary flag to experiment to enable compatibility with React's unstable <Activity> API
5656
ENABLE_ACTIVITY_COMPATIBILITY: boolean,
5757

58+
// Enables optimization for recreating the load more function.
59+
// When enabled, this flag reduce and simplify amount of dependencies for the function loadMore
60+
OPTIMIZE_RECREATING_LOAD_MORE_FUNCTION: boolean,
61+
5862
// Adds a prefix to the storage key of read time resolvers. This is used to
5963
// disambiguate the same resolver being used at both read time and exec time.
6064
ENABLE_READ_TIME_RESOLVER_STORAGE_KEY_PREFIX: boolean,
@@ -86,6 +90,7 @@ const RelayFeatureFlags: FeatureFlags = {
8690
ENABLE_RELAY_OPERATION_TRACKER_SUSPENSE: false,
8791
PROCESS_OPTIMISTIC_UPDATE_BEFORE_SUBSCRIPTION: false,
8892
MARK_RESOLVER_VALUES_AS_CLEAN_AFTER_FRAGMENT_REREAD: false,
93+
OPTIMIZE_RECREATING_LOAD_MORE_FUNCTION: false,
8994
ENABLE_CYLE_DETECTION_IN_VARIABLES: false,
9095
ENABLE_ACTIVITY_COMPATIBILITY: false,
9196
ENABLE_READ_TIME_RESOLVER_STORAGE_KEY_PREFIX: true,

0 commit comments

Comments
 (0)