Skip to content

Commit da7dbb9

Browse files
authored
Merge pull request #730 from data-driven-forms/fix-select-is-mounted
fix(common): use isMounted.current in select
2 parents c6f92eb + f5cf21c commit da7dbb9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/common/src/select/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const Select = ({
5151
dispatch({ type: 'startLoading' });
5252

5353
return loadOptions().then((data) => {
54-
if (isMounted) {
54+
if (isMounted.current) {
5555
if (!noValueUpdates) {
5656
if (value && Array.isArray(value)) {
5757
const selectValue = value.filter((value) =>
@@ -118,7 +118,7 @@ const Select = ({
118118

119119
loadOptions(inputValue)
120120
.then((options) => {
121-
if (isMounted) {
121+
if (isMounted.current) {
122122
dispatch({
123123
type: 'setPromises',
124124
payload: { [inputValue]: false },

packages/pf3-component-mapper/src/files/select/select.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
55
import { optionsPropType } from '@data-driven-forms/common/src/prop-types-templates';
66
import fnToString from '@data-driven-forms/common/src/utils/fn-to-string';
77
import DataDrivenSelect from '@data-driven-forms/common/src/select';
8+
import useIsMounted from '@data-driven-forms/common/src/hooks/use-is-mounted';
89
import { DropdownButton } from 'patternfly-react';
910
import clsx from 'clsx';
1011

@@ -94,6 +95,7 @@ const Select = ({ input, loadOptions, ...props }) => {
9495
const [isOpen, setIsOpen] = useState(false);
9596
const [isFetching, setFetching] = useState(loadOptions ? true : false);
9697
const [options, setOptions] = useState(props.options || []);
98+
const isMounted = useIsMounted();
9799

98100
// common select controls the string of loadOptions and if the string changed, then it reloads options
99101
// however we are enhancing the loadOptions here so the string is always the same
@@ -111,10 +113,16 @@ const Select = ({ input, loadOptions, ...props }) => {
111113

112114
const loadOptionsEnhanced = loadOptions
113115
? (value) => {
114-
setFetching(true);
116+
if (isMounted.current) {
117+
setFetching(true);
118+
}
119+
115120
return loadOptions(value).then((data) => {
116-
setOptions([...options, ...data.filter(({ value }) => !options.find((option) => option.value === value))]);
117-
setFetching(false);
121+
if (isMounted.current) {
122+
setOptions([...options, ...data.filter(({ value }) => !options.find((option) => option.value === value))]);
123+
setFetching(false);
124+
}
125+
118126
return data;
119127
});
120128
}

0 commit comments

Comments
 (0)