@@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
55import { optionsPropType } from '@data-driven-forms/common/src/prop-types-templates' ;
66import fnToString from '@data-driven-forms/common/src/utils/fn-to-string' ;
77import DataDrivenSelect from '@data-driven-forms/common/src/select' ;
8+ import useIsMounted from '@data-driven-forms/common/src/hooks/use-is-mounted' ;
89import { DropdownButton } from 'patternfly-react' ;
910import clsx from 'clsx' ;
1011
@@ -94,6 +95,7 @@ const Select = ({ input, loadOptions, ...props }) => {
9495 const [ isOpen , setIsOpen ] = useState ( false ) ;
9596 const [ isFetching , setFetching ] = useState ( loadOptions ? true : false ) ;
9697 const [ options , setOptions ] = useState ( props . options || [ ] ) ;
98+ const isMounted = useIsMounted ( ) ;
9799
98100 // common select controls the string of loadOptions and if the string changed, then it reloads options
99101 // however we are enhancing the loadOptions here so the string is always the same
@@ -111,10 +113,16 @@ const Select = ({ input, loadOptions, ...props }) => {
111113
112114 const loadOptionsEnhanced = loadOptions
113115 ? ( value ) => {
114- setFetching ( true ) ;
116+ if ( isMounted . current ) {
117+ setFetching ( true ) ;
118+ }
119+
115120 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+
118126 return data ;
119127 } ) ;
120128 }
0 commit comments