Skip to content

Commit 8f7f34b

Browse files
committed
Wrap all imperative calls into useEffect
1 parent 35bf164 commit 8f7f34b

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

x-pack/legacy/plugins/infra/public/containers/logs/with_stream_items.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ export const WithStreamItems = withStreamItems(
5252
>;
5353
initializeOnMount: boolean;
5454
}) => {
55-
const [shouldInitialize, setShouldInitialize] = useState(
56-
initializeOnMount && !props.isReloading && !props.isLoadingMore
57-
);
5855
const { currentHighlightKey, logEntryHighlightsById } = useContext(LogHighlightsState.Context);
5956
const items = useMemo(
6057
() =>
@@ -73,10 +70,16 @@ export const WithStreamItems = withStreamItems(
7370
]
7471
);
7572

76-
if (shouldInitialize) {
77-
setShouldInitialize(false);
78-
props.reloadEntries();
79-
}
73+
const [shouldInitialize, setShouldInitialize] = useState(
74+
initializeOnMount && !props.isReloading && !props.isLoadingMore
75+
);
76+
77+
useEffect(() => {
78+
if (shouldInitialize) {
79+
setShouldInitialize(false);
80+
props.reloadEntries();
81+
}
82+
}, [shouldInitialize, props]);
8083

8184
return children({
8285
...props,

x-pack/legacy/plugins/infra/public/utils/use_url_state.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
import { Location } from 'history';
8-
import { useMemo, useCallback, useState } from 'react';
8+
import { useCallback, useEffect, useMemo, useState } from 'react';
99
import { decode, encode, RisonValue } from 'rison-node';
10-
1110
import { QueryString } from 'ui/utils/query_string';
11+
1212
import { useHistory } from './history_context';
1313

1414
export const useUrlState = <State>({
@@ -42,10 +42,6 @@ export const useUrlState = <State>({
4242
urlStateString,
4343
]);
4444

45-
const [shouldInitialize, setShouldInitialize] = useState(
46-
writeDefaultState && typeof decodedState === 'undefined'
47-
);
48-
4945
const state = useMemo(() => (typeof decodedState !== 'undefined' ? decodedState : defaultState), [
5046
defaultState,
5147
decodedState,
@@ -74,10 +70,16 @@ export const useUrlState = <State>({
7470
[encodeUrlState, history, urlStateKey]
7571
);
7672

77-
if (shouldInitialize) {
78-
setShouldInitialize(false);
79-
setState(defaultState);
80-
}
73+
const [shouldInitialize, setShouldInitialize] = useState(
74+
writeDefaultState && typeof decodedState === 'undefined'
75+
);
76+
77+
useEffect(() => {
78+
if (shouldInitialize) {
79+
setShouldInitialize(false);
80+
setState(defaultState);
81+
}
82+
}, [shouldInitialize, setState, defaultState]);
8183

8284
return [state, setState] as [typeof state, typeof setState];
8385
};

0 commit comments

Comments
 (0)