Skip to content

Commit 1ff11f6

Browse files
authored
Merge pull request #161 from danybeltran/fix-isomporhiclayouteffect
fix(internal: `useIsomorphicLayoutEffect`):
2 parents 7fd19aa + 7943a06 commit 1ff11f6

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-react",
3-
"version": "3.0.4",
3+
"version": "3.0.5",
44
"description": "React hooks for data fetching",
55
"main": "dist/index.js",
66
"scripts": {

src/hooks/use-fetch.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import {
4747
createImperativeFetch,
4848
getMiliseconds,
4949
getTimePassed,
50-
revalidate
50+
revalidate,
51+
useIsomorphicLayoutEffect
5152
} from '../utils'
5253
import {
5354
createRequestFn,
@@ -1207,7 +1208,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
12071208
}
12081209
}
12091210

1210-
React.useLayoutEffect(() => {
1211+
useIsomorphicLayoutEffect(() => {
12111212
if (url !== '') {
12121213
if (!jsonCompare(previousProps[resolvedKey], optionsConfig)) {
12131214
abortControllers[resolvedKey]?.abort()
@@ -1236,7 +1237,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
12361237
}
12371238
}
12381239

1239-
React.useLayoutEffect(() => {
1240+
useIsomorphicLayoutEffect(() => {
12401241
if (!runningRequests[resolvedKey] && isExpired) {
12411242
if (windowExists) {
12421243
if (canRevalidate && url !== '') {
@@ -1254,7 +1255,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
12541255
}
12551256
}, [resolvedKey, serialize(optionsConfig), canRevalidate, thisDeps])
12561257

1257-
React.useLayoutEffect(() => {
1258+
useIsomorphicLayoutEffect(() => {
12581259
const revalidateAfterUnmount = revalidateOnMount
12591260
? true
12601261
: previousConfig[resolvedKey] !== serialize(optionsConfig)

src/utils/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ import {
2323
FetchInit,
2424
TimeSpan
2525
} from '../types'
26-
import { hasBaseUrl, isDefined, isFunction, queue, serialize } from './shared'
26+
import {
27+
hasBaseUrl,
28+
isDefined,
29+
isFunction,
30+
queue,
31+
serialize,
32+
windowExists
33+
} from './shared'
2734

2835
export function getMiliseconds(v: TimeSpan): number {
2936
if (typeof v === 'number') return v
@@ -82,6 +89,10 @@ export const createImperativeFetch = (ctx: FetchContextType) => {
8289
} as ImperativeFetch
8390
}
8491

92+
export const useIsomorphicLayoutEffect = windowExists
93+
? React.useLayoutEffect
94+
: React.useEffect
95+
8596
/**
8697
* Revalidate requests that match an id or ids
8798
*/

0 commit comments

Comments
 (0)