@@ -50,6 +50,9 @@ type State = {
50
50
loadedInputValue ?: string ,
51
51
loadedOptions : OptionsType ,
52
52
passEmptyOptions : boolean ,
53
+ optionsCache : { [ string ] : OptionsType } ,
54
+ prevDefaultOptions : OptionsType | boolean | void ,
55
+ prevCacheOptions : any | void ,
53
56
} ;
54
57
55
58
export const makeAsyncSelect = < C : { } > (
@@ -60,7 +63,6 @@ export const makeAsyncSelect = <C: {}>(
60
63
select : ElementRef < * > ;
61
64
lastRequest : { } ;
62
65
mounted : boolean = false ;
63
- optionsCache : { [ string ] : OptionsType } = { } ;
64
66
constructor ( props : C & AsyncProps ) {
65
67
super ( ) ;
66
68
this . state = {
@@ -72,6 +74,31 @@ export const makeAsyncSelect = <C: {}>(
72
74
isLoading : props . defaultOptions === true ,
73
75
loadedOptions : [ ] ,
74
76
passEmptyOptions : false ,
77
+ optionsCache : { } ,
78
+ prevDefaultOptions : undefined ,
79
+ prevCacheOptions : undefined ,
80
+ } ;
81
+ }
82
+ static getDerivedStateFromProps(props: C & AsyncProps , state : State ) {
83
+ const newCacheOptionsState =
84
+ props . cacheOptions !== state . prevCacheOptions
85
+ ? {
86
+ prevCacheOptions : props . cacheOptions ,
87
+ optionsCache : { } ,
88
+ }
89
+ : { } ;
90
+ const newDefaultOptionsState =
91
+ props . defaultOptions !== state . prevDefaultOptions
92
+ ? {
93
+ prevDefaultOptions : props . defaultOptions ,
94
+ defaultOptions : Array . isArray ( props . defaultOptions )
95
+ ? props . defaultOptions
96
+ : undefined ,
97
+ }
98
+ : { } ;
99
+ return {
100
+ ...newCacheOptionsState ,
101
+ ...newDefaultOptionsState ,
75
102
} ;
76
103
}
77
104
componentDidMount ( ) {
@@ -86,19 +113,6 @@ export const makeAsyncSelect = <C: {}>(
86
113
} ) ;
87
114
}
88
115
}
89
- UNSAFE_componentWillReceiveProps ( nextProps : C & AsyncProps ) {
90
- // if the cacheOptions prop changes, clear the cache
91
- if ( nextProps . cacheOptions !== this . props . cacheOptions ) {
92
- this . optionsCache = { } ;
93
- }
94
- if (nextProps.defaultOptions !== this.props.defaultOptions) {
95
- this . setState ( {
96
- defaultOptions : Array . isArray ( nextProps . defaultOptions )
97
- ? nextProps . defaultOptions
98
- : undefined ,
99
- } ) ;
100
- }
101
- }
102
116
componentWillUnmount ( ) {
103
117
this . mounted = false ;
104
118
}
@@ -131,11 +145,11 @@ export const makeAsyncSelect = <C: {}>(
131
145
} ) ;
132
146
return ;
133
147
}
134
- if (cacheOptions && this . optionsCache [ inputValue ] ) {
148
+ if ( cacheOptions && this . state . optionsCache [ inputValue ] ) {
135
149
this . setState ( {
136
150
inputValue,
137
151
loadedInputValue : inputValue ,
138
- loadedOptions : this . optionsCache [ inputValue ] ,
152
+ loadedOptions : this . state . optionsCache [ inputValue ] ,
139
153
isLoading : false ,
140
154
passEmptyOptions : false ,
141
155
} ) ;
@@ -150,17 +164,17 @@ export const makeAsyncSelect = <C: {}>(
150
164
( ) => {
151
165
this . loadOptions ( inputValue , options => {
152
166
if ( ! this . mounted ) return ;
153
- if ( options ) {
154
- this . optionsCache [ inputValue ] = options ;
155
- }
156
167
if ( request !== this . lastRequest ) return ;
157
168
delete this . lastRequest ;
158
- this . setState ( {
169
+ this . setState ( state => ( {
159
170
isLoading : false ,
160
171
loadedInputValue : inputValue ,
161
172
loadedOptions : options || [ ] ,
162
173
passEmptyOptions : false ,
163
- } ) ;
174
+ optionsCache : options
175
+ ? { ...state . optionsCache , [ inputValue ] : options }
176
+ : state . optionsCache ,
177
+ } ) ) ;
164
178
} ) ;
165
179
}
166
180
) ;
0 commit comments