Skip to content

Commit

Permalink
[EuiIcon] Fix setState errors when in React.StrictMode (#5899)
Browse files Browse the repository at this point in the history
* fix(icon): early setState warning from Strict Mode

* docs: update changelog with #4874

* use this.state.icon in didMount

* Changelog

Co-authored-by: Phil Tietjen <tietjen.philip@gmail.com>
  • Loading branch information
Constance and Phizzard authored May 12, 2022
1 parent 5a75a75 commit 5579e1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -588,23 +588,31 @@ export class EuiIcon extends PureComponent<EuiIconProps, State> {

const { type } = props;
const initialIcon = getInitialIcon(type);
let isLoading = false;

if (isEuiIconType(type) && initialIcon == null) {
isLoading = true;
this.loadIconComponent(type);
} else {
this.onIconLoad();
}

this.state = {
icon: initialIcon,
iconTitle: undefined,
isLoading,
neededLoading: isLoading,
isLoading: false,
neededLoading: false,
};
}

componentDidMount() {
const { type } = this.props;

if (isEuiIconType(type) && this.state.icon == null) {
//eslint-disable-next-line react/no-did-mount-set-state
this.setState({
neededLoading: true,
isLoading: true,
});

this.loadIconComponent(type);
} else {
this.onIconLoad();
}
}

componentDidUpdate(prevProps: EuiIconProps) {
const { type } = this.props;
if (type !== prevProps.type) {
Expand Down
3 changes: 3 additions & 0 deletions upcoming_changelogs/5899.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed `EuiIcon` from producing console warning in `React.StrictMode`

0 comments on commit 5579e1e

Please sign in to comment.