3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import React , { useEffect , useState , useContext } from 'react' ;
6
+ import React , { useEffect , useState , useContext , useRef } from 'react' ;
7
7
import {
8
8
EuiTitle ,
9
9
EuiSpacer ,
@@ -53,6 +53,7 @@ export const WLMCreate = ({
53
53
( memThreshold != null && memThreshold > 0 && memThreshold <= 100 ) ) ;
54
54
const [ loading , setLoading ] = useState ( false ) ;
55
55
const { dataSource, setDataSource } = useContext ( DataSourceContext ) ! ;
56
+ const isMounted = useRef ( true ) ;
56
57
57
58
useEffect ( ( ) => {
58
59
core . chrome . setBreadcrumbs ( [
@@ -68,6 +69,12 @@ export const WLMCreate = ({
68
69
] ) ;
69
70
} , [ core . chrome , history ] ) ;
70
71
72
+ useEffect ( ( ) => {
73
+ return ( ) => {
74
+ isMounted . current = false ;
75
+ } ;
76
+ } , [ ] ) ;
77
+
71
78
const handleCreate = async ( ) => {
72
79
setLoading ( true ) ;
73
80
try {
@@ -102,14 +109,17 @@ export const WLMCreate = ({
102
109
103
110
core . notifications . toasts . addSuccess ( `Workload group "${ name } " created successfully.` ) ;
104
111
history . push ( '/workloadManagement' ) ;
112
+ return ;
105
113
} catch ( err ) {
106
114
console . error ( err ) ;
107
115
core . notifications . toasts . addDanger ( {
108
116
title : 'Failed to create workload group' ,
109
117
text : err ?. body ?. message || 'Something went wrong' ,
110
118
} ) ;
111
119
} finally {
112
- setLoading ( false ) ;
120
+ if ( isMounted . current ) {
121
+ setLoading ( false ) ;
122
+ }
113
123
}
114
124
} ;
115
125
0 commit comments