@@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
5
5
import { optionsPropType } from '@data-driven-forms/common/src/prop-types-templates' ;
6
6
import fnToString from '@data-driven-forms/common/src/utils/fn-to-string' ;
7
7
import DataDrivenSelect from '@data-driven-forms/common/src/select' ;
8
+ import useIsMounted from '@data-driven-forms/common/src/hooks/use-is-mounted' ;
8
9
import { DropdownButton } from 'patternfly-react' ;
9
10
import clsx from 'clsx' ;
10
11
@@ -94,6 +95,7 @@ const Select = ({ input, loadOptions, ...props }) => {
94
95
const [ isOpen , setIsOpen ] = useState ( false ) ;
95
96
const [ isFetching , setFetching ] = useState ( loadOptions ? true : false ) ;
96
97
const [ options , setOptions ] = useState ( props . options || [ ] ) ;
98
+ const isMounted = useIsMounted ( ) ;
97
99
98
100
// common select controls the string of loadOptions and if the string changed, then it reloads options
99
101
// however we are enhancing the loadOptions here so the string is always the same
@@ -111,10 +113,16 @@ const Select = ({ input, loadOptions, ...props }) => {
111
113
112
114
const loadOptionsEnhanced = loadOptions
113
115
? ( value ) => {
114
- setFetching ( true ) ;
116
+ if ( isMounted . current ) {
117
+ setFetching ( true ) ;
118
+ }
119
+
115
120
return loadOptions ( value ) . then ( ( data ) => {
116
- setOptions ( [ ...options , ...data . filter ( ( { value } ) => ! options . find ( ( option ) => option . value === value ) ) ] ) ;
117
- setFetching ( false ) ;
121
+ if ( isMounted . current ) {
122
+ setOptions ( [ ...options , ...data . filter ( ( { value } ) => ! options . find ( ( option ) => option . value === value ) ) ] ) ;
123
+ setFetching ( false ) ;
124
+ }
125
+
118
126
return data ;
119
127
} ) ;
120
128
}
0 commit comments