Skip to content

Commit 208c184

Browse files
committed
Use useIsMounted in common select
1 parent d7b9b51 commit 208c184

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { useRef, useEffect } from 'react';
2+
3+
const useIsMounted = () => {
4+
const isMounted = useRef(false);
5+
useEffect(() => {
6+
isMounted.current = true;
7+
return () => (isMounted.current = false);
8+
}, []);
9+
return isMounted;
10+
};
11+
12+
export default useIsMounted;

packages/common/src/select/index.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import isEqual from 'lodash/isEqual';
99
import { input } from '../prop-types-templates';
1010
import fnToString from '../utils/fn-to-string';
1111
import reducer from './reducer';
12+
import useIsMounted from '../hooks/use-is-mounted';
1213

1314
const getSelectValue = (stateValue, simpleValue, isMulti, allOptions) =>
1415
simpleValue ? allOptions.filter(({ value }) => (isMulti ? stateValue.includes(value) : isEqual(value, stateValue))) : stateValue;
@@ -46,23 +47,27 @@ const Select = ({
4647
isLoading: false,
4748
options: propsOptions,
4849
promises: {},
49-
isMounted: false
50+
isInitialLoaded: false
5051
});
5152

53+
const isMounted = useIsMounted();
54+
5255
const updateOptions = () => {
5356
dispatch({ type: 'startLoading' });
5457

5558
return loadOptions().then((data) => {
56-
if (value && Array.isArray(value)) {
57-
const selectValue = value.filter((value) =>
58-
typeof value === 'object' ? data.find((option) => value.value === option.value) : data.find((option) => value === option.value)
59-
);
60-
onChange(selectValue.length === 0 ? undefined : selectValue);
61-
} else if (value && !data.find(({ value: internalValue }) => internalValue === value)) {
62-
onChange(undefined);
59+
if (isMounted) {
60+
if (value && Array.isArray(value)) {
61+
const selectValue = value.filter((value) =>
62+
typeof value === 'object' ? data.find((option) => value.value === option.value) : data.find((option) => value === option.value)
63+
);
64+
onChange(selectValue.length === 0 ? undefined : selectValue);
65+
} else if (value && !data.find(({ value: internalValue }) => internalValue === value)) {
66+
onChange(undefined);
67+
}
68+
69+
dispatch({ type: 'updateOptions', payload: data });
6370
}
64-
65-
dispatch({ type: 'updateOptions', payload: data });
6671
});
6772
};
6873

@@ -71,23 +76,19 @@ const Select = ({
7176
updateOptions();
7277
}
7378

74-
dispatch({ type: 'mounted' });
75-
76-
return () => {
77-
dispatch({ type: 'unmounted' });
78-
};
79+
dispatch({ type: 'initialLoaded' });
7980
}, []);
8081

8182
const loadOptionsStr = loadOptions ? fnToString(loadOptions) : '';
8283

8384
useEffect(() => {
84-
if (loadOptionsStr && state.isMounted) {
85+
if (loadOptionsStr && state.isInitialLoaded) {
8586
updateOptions();
8687
}
8788
}, [loadOptionsStr, loadOptionsChangeCounter]);
8889

8990
useEffect(() => {
90-
if (state.isMounted) {
91+
if (state.isInitialLoaded) {
9192
if (value && !propsOptions.map(({ value }) => value).includes(value)) {
9293
onChange(undefined);
9394
}
@@ -115,7 +116,7 @@ const Select = ({
115116

116117
loadOptions(inputValue)
117118
.then((options) => {
118-
if (state.isMounted) {
119+
if (isMounted) {
119120
dispatch({
120121
type: 'setPromises',
121122
payload: { [inputValue]: false },

packages/common/src/select/reducer.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ const reducer = (state, { type, payload, options = [] }) => {
2222
...state,
2323
options: payload
2424
};
25-
case 'mounted':
25+
case 'initialLoaded':
2626
return {
2727
...state,
28-
isMounted: true
29-
};
30-
case 'unmounted':
31-
return {
32-
...state,
33-
isMounted: false
28+
isInitialLoaded: true
3429
};
3530
case 'setPromises':
3631
return {

0 commit comments

Comments
 (0)