Skip to content

Commit 0657bb9

Browse files
committed
fix: block running onChange during init
1 parent 9307954 commit 0657bb9

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

static/js/publisher/components/ComboBox/ComboBox.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,22 @@ const ComboBox: FC<ComboBoxProps> = ({
169169
});
170170
}, [value, options]);
171171

172-
const firstRenderRef = useRef(true);
172+
// we don't run onChange until after we actually render some real options to choose from, this
173+
// avoids running it multiple times with and empty value during init (e.g. when fetching the
174+
// options from an API)
175+
const isOnchangeDisabledRef = useRef(true);
173176

174177
// Run the onChange callback when we change the selectedItem. We don't pass the callback as a
175178
// Downshift prop because it wouldn't run when we set the selectedItem inside the state reducer
176179
useEffect(() => {
177-
if (firstRenderRef.current) {
178-
firstRenderRef.current = false;
180+
if (isOnchangeDisabledRef.current) {
181+
if (options?.length > 0) {
182+
isOnchangeDisabledRef.current = false;
183+
}
179184
return;
180185
}
181186

182-
if (onChange) {
183-
onChange?.(comboBoxState.selectedItem?.value ?? null);
184-
}
187+
onChange?.(comboBoxState.selectedItem?.value ?? null);
185188
}, [comboBoxState.selectedItem?.value]);
186189

187190
return (

0 commit comments

Comments
 (0)