Skip to content

Commit 08396ec

Browse files
breadadamsKikobeats
authored andcommitted
fix: main loading flag and mini optimizations (#159)
+ Ensure that `loadingState` is set to true when `fetchData` is called + Add `useMemo` and `useCallback` hooks
1 parent c494559 commit 08396ec

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

packages/react/src/index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react'
1+
import React, { useState, useEffect, useCallback, useMemo } from 'react'
22
import PropTypes from 'prop-types'
33

44
import { CardWrap, CardMedia, CardContent, CardEmpty } from './components/Card'
@@ -44,29 +44,31 @@ function Microlink (props) {
4444
lazy,
4545
...restProps
4646
} = props
47-
const isLoadingUndefined = loadingProp === undefined
47+
const isLoadingUndefined = useMemo(() => loadingProp === undefined, [loadingProp])
4848
const [loadingState, setLoading] = useState(
4949
isLoadingUndefined ? true : loadingProp
5050
)
5151
const [cardData, setCardData] = useState({})
52-
const apiUrl = createApiUrl(props)
52+
const apiUrl = useMemo(() => createApiUrl(props), [props])
5353

54-
const isLazyEnabled = isLazySupported && (lazy === true || isObject(lazy))
55-
const lazyOptions = isObject(lazy) ? lazy : undefined
54+
const isLazyEnabled = useMemo(() => isLazySupported && (lazy === true || isObject(lazy)), [lazy])
55+
const lazyOptions = useMemo(() => isObject(lazy) ? lazy : undefined, [lazy])
5656
const [hasIntersected, cardRef] = useIntersectionObserver(isLazyEnabled, lazyOptions)
5757

58-
const canFetchData = !isLazyEnabled || (isLazyEnabled && hasIntersected)
58+
const canFetchData = useMemo(() => !isLazyEnabled || (isLazyEnabled && hasIntersected), [isLazyEnabled, hasIntersected])
5959

60-
const fetchData = () => {
60+
const fetchData = useCallback(() => {
6161
if (canFetchData) {
62+
setLoading(true)
6263
const fetch = isFunction(setData)
6364
? Promise.resolve({})
64-
: fetchFromApiUrl(apiUrl, props)
65+
: fetchFromApiUrl(apiUrl, props.apiKey)
66+
6567
fetch.then(({ data }) => mergeData(data))
6668
}
67-
}
69+
}, [apiUrl, canFetchData, setData, props.apiKey])
6870

69-
const mergeData = fetchData => {
71+
const mergeData = useCallback(fetchData => {
7072
const payload = isFunction(setData)
7173
? setData(fetchData)
7274
: { ...fetchData, ...setData }
@@ -102,7 +104,7 @@ function Microlink (props) {
102104
})
103105

104106
setLoading(false)
105-
}
107+
}, [setData])
106108

107109
useEffect(fetchData, [props.url, setData, hasIntersected])
108110

packages/react/src/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const createApiUrl = props => {
6969
return url
7070
}
7171

72-
export const fetchFromApiUrl = (apiUrl, { apiKey }) => {
72+
export const fetchFromApiUrl = (apiUrl, apiKey) => {
7373
const headers = apiKey ? { 'x-api-key': apiKey } : {}
7474
return fetch(apiUrl, { headers }).then(res => res.json())
7575
}

0 commit comments

Comments
 (0)