Skip to content

Commit 28ee545

Browse files
authored
Merge branch 'master' into findDOMNode-ScrollManager
2 parents 6db74e1 + 0e4634b commit 28ee545

File tree

5 files changed

+363
-277
lines changed

5 files changed

+363
-277
lines changed

.changeset/beige-eagles-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-select": patch
3+
---
4+
5+
Removed memoization of buildMenuOptions

.changeset/purple-moons-promise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-select": major
3+
---
4+
5+
Removed usages of UNSAFE React methods

packages/react-select/src/Async.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type State = {
5050
loadedInputValue?: string,
5151
loadedOptions: OptionsType,
5252
passEmptyOptions: boolean,
53+
optionsCache: { [string]: OptionsType },
54+
prevDefaultOptions: OptionsType | boolean | void,
55+
prevCacheOptions: any | void,
5356
};
5457

5558
export const makeAsyncSelect = <C: {}>(
@@ -60,7 +63,6 @@ export const makeAsyncSelect = <C: {}>(
6063
select: ElementRef<*>;
6164
lastRequest: {};
6265
mounted: boolean = false;
63-
optionsCache: { [string]: OptionsType } = {};
6466
constructor(props: C & AsyncProps) {
6567
super();
6668
this.state = {
@@ -72,6 +74,31 @@ export const makeAsyncSelect = <C: {}>(
7274
isLoading: props.defaultOptions === true,
7375
loadedOptions: [],
7476
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,
75102
};
76103
}
77104
componentDidMount() {
@@ -86,19 +113,6 @@ export const makeAsyncSelect = <C: {}>(
86113
});
87114
}
88115
}
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-
}
102116
componentWillUnmount() {
103117
this.mounted = false;
104118
}
@@ -131,11 +145,11 @@ export const makeAsyncSelect = <C: {}>(
131145
});
132146
return;
133147
}
134-
if (cacheOptions && this.optionsCache[inputValue]) {
148+
if (cacheOptions && this.state.optionsCache[inputValue]) {
135149
this.setState({
136150
inputValue,
137151
loadedInputValue: inputValue,
138-
loadedOptions: this.optionsCache[inputValue],
152+
loadedOptions: this.state.optionsCache[inputValue],
139153
isLoading: false,
140154
passEmptyOptions: false,
141155
});
@@ -150,17 +164,17 @@ export const makeAsyncSelect = <C: {}>(
150164
() => {
151165
this.loadOptions(inputValue, options => {
152166
if (!this.mounted) return;
153-
if (options) {
154-
this.optionsCache[inputValue] = options;
155-
}
156167
if (request !== this.lastRequest) return;
157168
delete this.lastRequest;
158-
this.setState({
169+
this.setState(state => ({
159170
isLoading: false,
160171
loadedInputValue: inputValue,
161172
loadedOptions: options || [],
162173
passEmptyOptions: false,
163-
});
174+
optionsCache: options
175+
? { ...state.optionsCache, [inputValue]: options }
176+
: state.optionsCache,
177+
}));
164178
});
165179
}
166180
);

packages/react-select/src/Creatable.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export const makeCreatableSelect = <C: {}>(
9999
options: options,
100100
};
101101
}
102-
UNSAFE_componentWillReceiveProps(nextProps: CreatableProps & C) {
102+
static getDerivedStateFromProps(props: CreatableProps & C, state: State) {
103103
const {
104104
allowCreateWhileLoading,
105105
createOptionPosition,
@@ -109,23 +109,23 @@ export const makeCreatableSelect = <C: {}>(
109109
isLoading,
110110
isValidNewOption,
111111
value,
112-
} = nextProps;
113-
const options = nextProps.options || [];
114-
let { newOption } = this.state;
112+
} = props;
113+
const options = props.options || [];
114+
let { newOption } = state;
115115
if (isValidNewOption(inputValue, cleanValue(value), options)) {
116116
newOption = getNewOptionData(inputValue, formatCreateLabel(inputValue));
117117
} else {
118118
newOption = undefined;
119119
}
120-
this.setState({
120+
return {
121121
newOption: newOption,
122122
options:
123123
(allowCreateWhileLoading || !isLoading) && newOption
124124
? createOptionPosition === 'first'
125125
? [newOption, ...options]
126126
: [...options, newOption]
127127
: options,
128-
});
128+
};
129129
}
130130
onChange = (newValue: ValueType, actionMeta: ActionMeta) => {
131131
const {

0 commit comments

Comments
 (0)