1
- import { useEffect , useReducer } from 'react' ;
1
+ import { useEffect , useReducer , useState } from 'react' ;
2
2
3
3
import isEqual from 'lodash/isEqual' ;
4
4
@@ -58,7 +58,7 @@ const handleSelectChange = (option, simpleValue, isMulti, onChange, allOptions,
58
58
const useSelect = ( {
59
59
loadOptions,
60
60
optionsTransformer,
61
- options : propsOptions ,
61
+ options : initialOptions = [ ] ,
62
62
noValueUpdates,
63
63
onChange,
64
64
value,
@@ -69,8 +69,15 @@ const useSelect = ({
69
69
simpleValue,
70
70
compareValues,
71
71
} ) => {
72
- const [ state , originalDispatch ] = useReducer ( reducer , { optionsTransformer, propsOptions } , init ) ;
73
- const dispatch = ( action ) => originalDispatch ( { ...action , optionsTransformer } ) ;
72
+ const [ propsOptions , setPropsCache ] = useState ( initialOptions ) ;
73
+ const [ state , originalDispatch ] = useReducer ( reducer , { optionsTransformer, propsOptions : initialOptions } , init ) ;
74
+ const dispatch = ( action ) => originalDispatch ( { ...action , optionsTransformer, compareValues } ) ;
75
+
76
+ useEffect ( ( ) => {
77
+ if ( ! isEqual ( initialOptions , propsOptions ) ) {
78
+ setPropsCache ( initialOptions ) ;
79
+ }
80
+ } , [ initialOptions ] ) ;
74
81
75
82
const isMounted = useIsMounted ( ) ;
76
83
@@ -114,7 +121,7 @@ const useSelect = ({
114
121
} , [ loadOptionsStr , loadOptionsChangeCounter ] ) ;
115
122
116
123
useEffect ( ( ) => {
117
- if ( state . isInitialLoaded ) {
124
+ if ( ! isEqual ( state . options , propsOptions ) && state . isInitialLoaded ) {
118
125
if ( ! noValueUpdates && value && ! propsOptions . map ( ( { value } ) => value ) . includes ( value ) ) {
119
126
onChange ( undefined ) ;
120
127
}
@@ -125,14 +132,14 @@ const useSelect = ({
125
132
126
133
const onInputChange = ( inputValue ) => {
127
134
if ( inputValue && loadOptions && state . promises [ inputValue ] === undefined && isSearchable ) {
128
- dispatch ( { type : 'setPromises' , payload : { [ inputValue ] : true , compareValues } } ) ;
135
+ dispatch ( { type : 'setPromises' , payload : { [ inputValue ] : true } } ) ;
129
136
130
137
loadOptions ( inputValue )
131
138
. then ( ( options ) => {
132
139
if ( isMounted . current ) {
133
140
dispatch ( {
134
141
type : 'setPromises' ,
135
- payload : { [ inputValue ] : false , compareValues } ,
142
+ payload : { [ inputValue ] : false } ,
136
143
options,
137
144
} ) ;
138
145
}
0 commit comments