Skip to content

Commit dc745bf

Browse files
committed
fix(common): memoize initial select options value
1 parent 03f6346 commit dc745bf

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/common/src/select/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Select = ({
1212
simpleValue = true,
1313
isMulti,
1414
pluckSingleValue = true,
15-
options: propsOptions = [],
15+
options: propsOptions,
1616
loadOptions,
1717
loadingMessage,
1818
placeholder = 'Choose...',

packages/common/src/use-select/use-select.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useReducer } from 'react';
1+
import { useEffect, useReducer, useState } from 'react';
22

33
import isEqual from 'lodash/isEqual';
44

@@ -58,7 +58,7 @@ const handleSelectChange = (option, simpleValue, isMulti, onChange, allOptions,
5858
const useSelect = ({
5959
loadOptions,
6060
optionsTransformer,
61-
options: propsOptions,
61+
options: initialOptions = [],
6262
noValueUpdates,
6363
onChange,
6464
value,
@@ -69,9 +69,16 @@ const useSelect = ({
6969
simpleValue,
7070
compareValues,
7171
}) => {
72-
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions }, init);
72+
const [propsOptions, setPropsCache] = useState(initialOptions);
73+
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions: initialOptions }, init);
7374
const dispatch = (action) => originalDispatch({ ...action, optionsTransformer });
7475

76+
useEffect(() => {
77+
if (!isEqual(initialOptions, propsOptions)) {
78+
setPropsCache(initialOptions);
79+
}
80+
}, [initialOptions]);
81+
7582
const isMounted = useIsMounted();
7683

7784
const updateOptions = () => {

0 commit comments

Comments
 (0)