1- import { useEffect , useReducer } from 'react' ;
1+ import { useEffect , useReducer , useState } from 'react' ;
22
33import isEqual from 'lodash/isEqual' ;
44
@@ -58,7 +58,7 @@ const handleSelectChange = (option, simpleValue, isMulti, onChange, allOptions,
5858const useSelect = ( {
5959 loadOptions,
6060 optionsTransformer,
61- options : propsOptions ,
61+ options : initialOptions = [ ] ,
6262 noValueUpdates,
6363 onChange,
6464 value,
@@ -69,8 +69,15 @@ const useSelect = ({
6969 simpleValue,
7070 compareValues,
7171} ) => {
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 ] ) ;
7481
7582 const isMounted = useIsMounted ( ) ;
7683
@@ -114,7 +121,7 @@ const useSelect = ({
114121 } , [ loadOptionsStr , loadOptionsChangeCounter ] ) ;
115122
116123 useEffect ( ( ) => {
117- if ( state . isInitialLoaded ) {
124+ if ( ! isEqual ( state . options , propsOptions ) && state . isInitialLoaded ) {
118125 if ( ! noValueUpdates && value && ! propsOptions . map ( ( { value } ) => value ) . includes ( value ) ) {
119126 onChange ( undefined ) ;
120127 }
@@ -125,14 +132,14 @@ const useSelect = ({
125132
126133 const onInputChange = ( inputValue ) => {
127134 if ( inputValue && loadOptions && state . promises [ inputValue ] === undefined && isSearchable ) {
128- dispatch ( { type : 'setPromises' , payload : { [ inputValue ] : true , compareValues } } ) ;
135+ dispatch ( { type : 'setPromises' , payload : { [ inputValue ] : true } } ) ;
129136
130137 loadOptions ( inputValue )
131138 . then ( ( options ) => {
132139 if ( isMounted . current ) {
133140 dispatch ( {
134141 type : 'setPromises' ,
135- payload : { [ inputValue ] : false , compareValues } ,
142+ payload : { [ inputValue ] : false } ,
136143 options,
137144 } ) ;
138145 }
0 commit comments