Skip to content

Commit

Permalink
fix(common): memoize initial select options value
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Mar 21, 2024
1 parent 2a9ace4 commit a3f8df1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Select = ({
simpleValue = true,
isMulti,
pluckSingleValue = true,
options: propsOptions = [],
options: propsOptions,
loadOptions,
loadingMessage,
placeholder = 'Choose...',
Expand Down
13 changes: 10 additions & 3 deletions packages/common/src/use-select/use-select.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useReducer } from 'react';
import { useEffect, useReducer, useState } from 'react';

import isEqual from 'lodash/isEqual';

Expand Down Expand Up @@ -58,7 +58,7 @@ const handleSelectChange = (option, simpleValue, isMulti, onChange, allOptions,
const useSelect = ({
loadOptions,
optionsTransformer,
options: propsOptions,
options: initialOptions = [],
noValueUpdates,
onChange,
value,
Expand All @@ -69,9 +69,16 @@ const useSelect = ({
simpleValue,
compareValues,
}) => {
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions }, init);
const [propsOptions, setPropsCache] = useState(initialOptions);
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions: initialOptions }, init);
const dispatch = (action) => originalDispatch({ ...action, optionsTransformer });

useEffect(() => {
if (!isEqual(initialOptions, propsOptions)) {
setPropsCache(initialOptions);
}
}, [initialOptions]);

const isMounted = useIsMounted();

const updateOptions = () => {
Expand Down

0 comments on commit a3f8df1

Please sign in to comment.