Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4fe4485
Remove UNSAFE_componentWillReceiveProps from Async
Methuselah96 Dec 9, 2020
edc14e4
Remove UNSAFE_componentWillReceiveProps from Creatable
Methuselah96 Dec 9, 2020
4494861
Apply automatic Prettier changes in Select
Methuselah96 Dec 9, 2020
869c10f
Move components to getComponents() getter
Methuselah96 Dec 9, 2020
56824dc
Make menuOptions calculated instead of storing it in state
Methuselah96 Dec 9, 2020
0258620
Move methods necessary for getDerivedStateFromProps out of class
Methuselah96 Dec 9, 2020
42d3518
Fix bug where the isOptionDisabled builtin was incorrectly called
Methuselah96 Dec 9, 2020
d2919c7
Refactor buildMenuOptions and move it out of the class
Methuselah96 Dec 9, 2020
c2a1137
Switch to getDerivedStateFromProps
Methuselah96 Dec 9, 2020
b2488bb
Create purple-moons-promise.md
Methuselah96 Dec 9, 2020
daaad67
Remove unnecessary state parameters
Methuselah96 Dec 9, 2020
c14f591
Preserve indices to match previous behavior
Methuselah96 Dec 9, 2020
13c2b6a
Memoize buildCategorizedOptions
Methuselah96 Dec 9, 2020
ebf1c04
Restore memoization of getComponents()
Methuselah96 Dec 10, 2020
2303fdf
Update changeset
Methuselah96 Dec 10, 2020
384ca8c
Memoize buildFocusableOptions as well
Methuselah96 Dec 10, 2020
c03527a
Merge branch 'master' into remove-unsafe-react
Methuselah96 Dec 11, 2020
52ec1c1
Update purple-moons-promise.md
Methuselah96 Dec 12, 2020
db13938
Revert "Restore memoization of getComponents()"
Methuselah96 Dec 10, 2020
896a845
Merge branch 'master' into remove-unsafe-react
Methuselah96 Dec 23, 2020
3977fc4
Merge branch 'master' into remove-unsafe-react
Methuselah96 Jan 20, 2021
a2e4d96
Changes from merge conflict
Methuselah96 Jan 20, 2021
1fec666
Fix typo
Methuselah96 Jan 20, 2021
7d7cbe4
Merge branch 'master' into remove-unsafe-react
JedWatson Jan 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purple-moons-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-select": major
---

Removed usages of UNSAFE React methods
56 changes: 35 additions & 21 deletions packages/react-select/src/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type State = {
loadedInputValue?: string,
loadedOptions: OptionsType,
passEmptyOptions: boolean,
optionsCache: { [string]: OptionsType },
prevDefaultOptions: OptionsType | boolean | void,
prevCacheOptions: any | void,
};

export const makeAsyncSelect = <C: {}>(
Expand All @@ -60,7 +63,6 @@ export const makeAsyncSelect = <C: {}>(
select: ElementRef<*>;
lastRequest: {};
mounted: boolean = false;
optionsCache: { [string]: OptionsType } = {};
constructor(props: C & AsyncProps) {
super();
this.state = {
Expand All @@ -72,6 +74,31 @@ export const makeAsyncSelect = <C: {}>(
isLoading: props.defaultOptions === true,
loadedOptions: [],
passEmptyOptions: false,
optionsCache: {},
prevDefaultOptions: undefined,
prevCacheOptions: undefined,
};
}
static getDerivedStateFromProps(props: C & AsyncProps, state: State) {
const newCacheOptionsState =
props.cacheOptions !== state.prevCacheOptions
? {
prevCacheOptions: props.cacheOptions,
optionsCache: {},
}
: {};
const newDefaultOptionsState =
props.defaultOptions !== state.prevDefaultOptions
? {
prevDefaultOptions: props.defaultOptions,
defaultOptions: Array.isArray(props.defaultOptions)
? props.defaultOptions
: undefined,
}
: {};
return {
...newCacheOptionsState,
...newDefaultOptionsState,
};
}
componentDidMount() {
Expand All @@ -86,19 +113,6 @@ export const makeAsyncSelect = <C: {}>(
});
}
}
UNSAFE_componentWillReceiveProps(nextProps: C & AsyncProps) {
// if the cacheOptions prop changes, clear the cache
if (nextProps.cacheOptions !== this.props.cacheOptions) {
this.optionsCache = {};
}
if (nextProps.defaultOptions !== this.props.defaultOptions) {
this.setState({
defaultOptions: Array.isArray(nextProps.defaultOptions)
? nextProps.defaultOptions
: undefined,
});
}
}
componentWillUnmount() {
this.mounted = false;
}
Expand Down Expand Up @@ -131,11 +145,11 @@ export const makeAsyncSelect = <C: {}>(
});
return;
}
if (cacheOptions && this.optionsCache[inputValue]) {
if (cacheOptions && this.state.optionsCache[inputValue]) {
this.setState({
inputValue,
loadedInputValue: inputValue,
loadedOptions: this.optionsCache[inputValue],
loadedOptions: this.state.optionsCache[inputValue],
isLoading: false,
passEmptyOptions: false,
});
Expand All @@ -150,17 +164,17 @@ export const makeAsyncSelect = <C: {}>(
() => {
this.loadOptions(inputValue, options => {
if (!this.mounted) return;
if (options) {
this.optionsCache[inputValue] = options;
}
if (request !== this.lastRequest) return;
delete this.lastRequest;
this.setState({
this.setState(state => ({
isLoading: false,
loadedInputValue: inputValue,
loadedOptions: options || [],
passEmptyOptions: false,
});
optionsCache: options
? { ...state.optionsCache, [inputValue]: options }
: state.optionsCache,
}));
});
}
);
Expand Down
12 changes: 6 additions & 6 deletions packages/react-select/src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const makeCreatableSelect = <C: {}>(
options: options,
};
}
UNSAFE_componentWillReceiveProps(nextProps: CreatableProps & C) {
static getDerivedStateFromProps(props: CreatableProps & C, state: State) {
const {
allowCreateWhileLoading,
createOptionPosition,
Expand All @@ -109,23 +109,23 @@ export const makeCreatableSelect = <C: {}>(
isLoading,
isValidNewOption,
value,
} = nextProps;
const options = nextProps.options || [];
let { newOption } = this.state;
} = props;
const options = props.options || [];
let { newOption } = state;
if (isValidNewOption(inputValue, cleanValue(value), options)) {
newOption = getNewOptionData(inputValue, formatCreateLabel(inputValue));
} else {
newOption = undefined;
}
this.setState({
return {
newOption: newOption,
options:
(allowCreateWhileLoading || !isLoading) && newOption
? createOptionPosition === 'first'
? [newOption, ...options]
: [...options, newOption]
: options,
});
};
}
onChange = (newValue: ValueType, actionMeta: ActionMeta) => {
const {
Expand Down
Loading