@@ -9,6 +9,7 @@ import isEqual from 'lodash/isEqual';
99import { input } from '../prop-types-templates' ;
1010import fnToString from '../utils/fn-to-string' ;
1111import reducer from './reducer' ;
12+ import useIsMounted from '../hooks/use-is-mounted' ;
1213
1314const getSelectValue = ( stateValue , simpleValue , isMulti , allOptions ) =>
1415 simpleValue ? allOptions . filter ( ( { value } ) => ( isMulti ? stateValue . includes ( value ) : isEqual ( value , stateValue ) ) ) : stateValue ;
@@ -46,23 +47,27 @@ const Select = ({
4647 isLoading : false ,
4748 options : propsOptions ,
4849 promises : { } ,
49- isMounted : false
50+ isInitialLoaded : false
5051 } ) ;
5152
53+ const isMounted = useIsMounted ( ) ;
54+
5255 const updateOptions = ( ) => {
5356 dispatch ( { type : 'startLoading' } ) ;
5457
5558 return loadOptions ( ) . then ( ( data ) => {
56- if ( value && Array . isArray ( value ) ) {
57- const selectValue = value . filter ( ( value ) =>
58- typeof value === 'object' ? data . find ( ( option ) => value . value === option . value ) : data . find ( ( option ) => value === option . value )
59- ) ;
60- onChange ( selectValue . length === 0 ? undefined : selectValue ) ;
61- } else if ( value && ! data . find ( ( { value : internalValue } ) => internalValue === value ) ) {
62- onChange ( undefined ) ;
59+ if ( isMounted ) {
60+ if ( value && Array . isArray ( value ) ) {
61+ const selectValue = value . filter ( ( value ) =>
62+ typeof value === 'object' ? data . find ( ( option ) => value . value === option . value ) : data . find ( ( option ) => value === option . value )
63+ ) ;
64+ onChange ( selectValue . length === 0 ? undefined : selectValue ) ;
65+ } else if ( value && ! data . find ( ( { value : internalValue } ) => internalValue === value ) ) {
66+ onChange ( undefined ) ;
67+ }
68+
69+ dispatch ( { type : 'updateOptions' , payload : data } ) ;
6370 }
64-
65- dispatch ( { type : 'updateOptions' , payload : data } ) ;
6671 } ) ;
6772 } ;
6873
@@ -71,23 +76,19 @@ const Select = ({
7176 updateOptions ( ) ;
7277 }
7378
74- dispatch ( { type : 'mounted' } ) ;
75-
76- return ( ) => {
77- dispatch ( { type : 'unmounted' } ) ;
78- } ;
79+ dispatch ( { type : 'initialLoaded' } ) ;
7980 } , [ ] ) ;
8081
8182 const loadOptionsStr = loadOptions ? fnToString ( loadOptions ) : '' ;
8283
8384 useEffect ( ( ) => {
84- if ( loadOptionsStr && state . isMounted ) {
85+ if ( loadOptionsStr && state . isInitialLoaded ) {
8586 updateOptions ( ) ;
8687 }
8788 } , [ loadOptionsStr , loadOptionsChangeCounter ] ) ;
8889
8990 useEffect ( ( ) => {
90- if ( state . isMounted ) {
91+ if ( state . isInitialLoaded ) {
9192 if ( value && ! propsOptions . map ( ( { value } ) => value ) . includes ( value ) ) {
9293 onChange ( undefined ) ;
9394 }
@@ -115,7 +116,7 @@ const Select = ({
115116
116117 loadOptions ( inputValue )
117118 . then ( ( options ) => {
118- if ( state . isMounted ) {
119+ if ( isMounted ) {
119120 dispatch ( {
120121 type : 'setPromises' ,
121122 payload : { [ inputValue ] : false } ,
0 commit comments