Skip to content

Commit a938ffd

Browse files
authored
fix(file-uploader-button): avoid state updates during render (#21534)
1 parent ed5633a commit a938ffd

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

packages/react/src/components/FileUploader/FileUploaderButton.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2025
2+
* Copyright IBM Corp. 2016, 2026
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

88
import cx from 'classnames';
99
import PropTypes from 'prop-types';
10-
import React, { useRef, useState, type HTMLAttributes } from 'react';
10+
import React, { useEffect, useRef, useState, type HTMLAttributes } from 'react';
1111
import { matches, keys } from '../../internal/keyboard';
1212
import { useId } from '../../internal/useId';
1313
import { usePrefix } from '../../internal/usePrefix';
@@ -121,7 +121,6 @@ function FileUploaderButton({
121121
}: FileUploaderButtonProps) {
122122
const prefix = usePrefix();
123123
const [labelText, setLabelText] = useState(ownerLabelText);
124-
const [prevOwnerLabelText, setPrevOwnerLabelText] = useState(ownerLabelText);
125124
const generatedId = useId();
126125
const { current: inputId } = useRef(id || generatedId);
127126
const inputNode = useRef<HTMLInputElement>(null);
@@ -134,11 +133,9 @@ function FileUploaderButton({
134133
[`${prefix}--layout--size-${size}`]: size,
135134
});
136135

137-
// Adjust label text state based on changes to the labelText prop
138-
if (ownerLabelText !== prevOwnerLabelText) {
136+
useEffect(() => {
139137
setLabelText(ownerLabelText);
140-
setPrevOwnerLabelText(ownerLabelText);
141-
}
138+
}, [ownerLabelText]);
142139

143140
function onClick(event) {
144141
event.target.value = null;

0 commit comments

Comments
 (0)