I am trying to update the state (tableColumnConfiguration
) inside useEffect
and then pass on that state to the child component, but this code throws a "Maximum update depth exceeded" error and the app freezes without being able to click anything on screen.
const[environmentTableColumns, setEnvironmentTableCoulmns] = useState(environmentConditionsColumns);
const {
data: conditionTypeData,
loading: loadingConditionTypeData,
errorRedirect: conditionTypeDataErrorRedirect
} = useSectionEnumQuery('conditionType'); // this is API call
useEffect(() => {
if (conditionTypeData) {
let data;
let updatedEnvironmentColumnConfiguration = environmentConditionsColumns;
updatedEnvironmentColumnConfiguration = updatedEnvironmentColumnConfiguration.map(item => {
if (item.dataIndex === 'conditionType') {
data = conditionTypeData;
}
return data
? {
...item,
render: text => {
return renderEnum(text, data);
}
}
: item;
});
setEnvironmentTableCoulmns(updatedEnvironmentColumnConfiguration); // here i am setting the state
}
}, [conditionTypeData])
Child component :
<SpaceTypeTable
values={values}
isReadOnly={isReadOnly}
isProjectSystem={isProjectSystem}
tableTitle="Environment Criteria"
mappedLibrarySourceArray="environments"
sourceRender={p => renderGuidelineItem(p, true)}
tableColumns={environmentTableColumns} // here i am passing the column configuration
section={MASTER_SECTIONS.LIBRARY_ENVIRONMENT}
guidelines={guidelines}
form={form}
handleWarning={handleWarning}
/>
What's causing this useEffect
loop?
Update : UseSectionEnumQuery :
export const useSectionEnumQuery = resultFieldName => {
const { data: result, loading, error } = useQuery(ENUM_TYPES(resultFieldName));
const data = result?.[resultFieldName] && sortBy(result[resultFieldName], o => o.label);
const errorRedirect = error && errorRedirectElement(error, resultFieldName);
return { loading, data, errorRedirect };
};