Skip to content

Commit

Permalink
fix: fetch is called too many times if req fails, because of always c…
Browse files Browse the repository at this point in the history
…hanging args
  • Loading branch information
teodorus-nathaniel committed Dec 28, 2023
1 parent abdb355 commit 07bd940
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/rtk/app/hooksCommon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AsyncThunkAction } from '@reduxjs/toolkit'
import { isEmptyArray, newLogger } from '@subsocial/utils'
import { useEffect, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { shallowEqual } from 'react-redux'
import useSubsocialEffect from 'src/components/api/useSubsocialEffect'
import {
Expand Down Expand Up @@ -52,6 +52,10 @@ export function useFetchWithoutApi<Args, Struct>(
const [error, setError] = useState<Error>()
const dispatch = useAppDispatch()

const jsonArgs = useMemo(() => {
return JSON.stringify(args)
}, [args])

const isEnabled = !!enabled
useEffect(() => {
if (!isEnabled) return
Expand All @@ -75,7 +79,7 @@ export function useFetchWithoutApi<Args, Struct>(
return () => {
isMounted = false
}
}, [dispatch, args, isEnabled])
}, [dispatch, jsonArgs, isEnabled])

return {
loading,
Expand Down
3 changes: 2 additions & 1 deletion src/rtk/features/creators/creatorsListHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { useFetchWithoutApi } from 'src/rtk/app/hooksCommon'
import { useAppSelector } from 'src/rtk/app/store'
import { fetchCreators, selectCreators } from './creatorsListSlice'

const emptyArgs = {}
export function useFetchCreators(config?: { enabled?: boolean }) {
const { enabled } = config || {}
const data = useAppSelector(state => selectCreators(state))

const props = useFetchWithoutApi(fetchCreators, {}, { enabled })
const props = useFetchWithoutApi(fetchCreators, emptyArgs, { enabled })

return {
...props,
Expand Down

0 comments on commit 07bd940

Please sign in to comment.