Skip to content

Commit

Permalink
NCL-6996 Remove ReactDOM.unstable_batchedUpdates calls
Browse files Browse the repository at this point in the history
  • Loading branch information
patrikk0123 committed Jul 8, 2024
1 parent 48bd47c commit 29e8659
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions src/hooks/useServiceContainer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AxiosError, AxiosRequestConfig, AxiosResponse, isAxiosError } from 'axios';
import { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';

import { backendErrorMessageMapper } from 'common/backendErrorMessageMapper';

Expand Down Expand Up @@ -132,22 +131,18 @@ export const useServiceContainer = <T extends TServiceData, U extends TServicePa
: (service as ServiceFunctionWithoutParams<T>)(requestConfig)
)
.then((response) => {
// In a future React version (potentially in React 17) this could be removed as it will be default behavior
// https://stackoverflow.com/questions/48563650/does-react-keep-the-order-for-state-updates/48610973#48610973
ReactDOM.unstable_batchedUpdates(() => {
setLoading(false);

/**
* Convert undefined to {@link DataValues.noData} as
* undefined is reserved for {@link DataValues.notYetData}
*/
if (response.data === undefined) {
setData(DataValues.noData);
} else {
setData(response.data);
}
setError(ERROR_INIT);
});
setLoading(false);

/**
* Convert undefined to {@link DataValues.noData} as
* undefined is reserved for {@link DataValues.notYetData}
*/
if (response.data === undefined) {
setData(DataValues.noData);
} else {
setData(response.data);
}
setError(ERROR_INIT);

return response;
})
Expand All @@ -157,13 +152,9 @@ export const useServiceContainer = <T extends TServiceData, U extends TServicePa
if (error.name !== 'CanceledError') {
// execute only for last request
if (loadingCount.current <= 1) {
// In a future React version (potentially in React 17) this could be removed as it will be default behavior
// https://stackoverflow.com/questions/48563650/does-react-keep-the-order-for-state-updates/48610973#48610973
ReactDOM.unstable_batchedUpdates(() => {
setLoading(false);
setError(errorMessage);
setData(DataValues.noData);
});
setLoading(false);
setError(errorMessage);
setData(DataValues.noData);
}
}

Expand Down

0 comments on commit 29e8659

Please sign in to comment.