Skip to content

Commit

Permalink
fix: use programId in effect dep list for fetching DHIS2-15752
Browse files Browse the repository at this point in the history
program is an object that changes at each render triggering the fetch
every time.
Instead, use a memoized program.id which is the only thing needed in the
fetch.
Memoize also setIsListEndVisible to avoid returning a difference
function from the hook.
  • Loading branch information
edoardo committed Oct 20, 2023
1 parent ddce619 commit 3493494
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ const useProgramDataDimensions = ({
},
dispatch,
] = useReducer(reducer, initialState)

const programId = useMemo(() => program.id, [program])
const programStageNames = useMemo(
() =>
program.programStages?.reduce((acc, stage) => {
Expand All @@ -222,11 +224,17 @@ const useProgramDataDimensions = ({
const nameProp =
currentUser.settings[DERIVED_USER_SETTINGS_DISPLAY_NAME_PROPERTY]

const setIsListEndVisible = (isVisible) => {
if (isVisible !== isListEndVisible) {
dispatch({ type: ACTIONS_SET_LIST_END_VISIBLE, payload: isVisible })
}
}
const setIsListEndVisible = useCallback(
(isVisible) => {
if (isVisible !== isListEndVisible) {
dispatch({
type: ACTIONS_SET_LIST_END_VISIBLE,
payload: isVisible,
})
}
},
[isListEndVisible]
)

const fetchDimensions = useCallback(
async (shouldReset) => {
Expand All @@ -243,7 +251,7 @@ const useProgramDataDimensions = ({
dimensions: createDimensionsQuery({
inputType,
page,
programId: program.id,
programId,
stageId,
searchTerm,
dimensionType,
Expand All @@ -267,20 +275,22 @@ const useProgramDataDimensions = ({
}
},
[
dimensions,
engine,
programStageNames,
inputType,
nextPage,
program,
programId,
stageId,
searchTerm,
dimensionType,
isListEndVisible,
nameProp,
]
)

useEffect(() => {
fetchDimensions(true)
}, [inputType, program, stageId, searchTerm, dimensionType, nameProp])
}, [inputType, programId, stageId, searchTerm, dimensionType, nameProp])

Check warning on line 293 in src/components/MainSidebar/ProgramDimensionsPanel/useProgramDataDimensions.js

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'fetchDimensions'. Either include it or remove the dependency array

useEffect(() => {
if (isListEndVisible && !isLastPage && !fetching) {
Expand Down

0 comments on commit 3493494

Please sign in to comment.