From 5b2a1ccbcbd4fe718ab0314ec08bc14635f878c2 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Tue, 11 Dec 2018 09:43:04 +0100 Subject: [PATCH] fix projects component to not set state when unmounted --- .../quality-gates/components/Projects.tsx | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/quality-gates/components/Projects.tsx b/server/sonar-web/src/main/js/apps/quality-gates/components/Projects.tsx index 279bc01bbe6..84105505fba 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/components/Projects.tsx +++ b/server/sonar-web/src/main/js/apps/quality-gates/components/Projects.tsx @@ -39,12 +39,18 @@ interface State { } export default class Projects extends React.PureComponent { + mounted = false; state: State = { projects: [], selectedProjects: [] }; componentDidMount() { + this.mounted = true; this.handleSearch('', Filter.Selected); } + componentWillUnmount() { + this.mounted = false; + } + handleSearch = (query: string, selected: string) => { return searchGates({ gateId: this.props.qualityGate.id, @@ -53,12 +59,14 @@ export default class Projects extends React.PureComponent { query: query !== '' ? query : undefined, selected }).then(data => { - this.setState({ - projects: data.results, - selectedProjects: data.results - .filter(project => project.selected) - .map(project => project.id) - }); + if (this.mounted) { + this.setState({ + projects: data.results, + selectedProjects: data.results + .filter(project => project.selected) + .map(project => project.id) + }); + } }); }; @@ -68,9 +76,11 @@ export default class Projects extends React.PureComponent { organization: this.props.organization, projectId: id }).then(() => { - this.setState(state => ({ - selectedProjects: [...state.selectedProjects, id] - })); + if (this.mounted) { + this.setState(state => ({ + selectedProjects: [...state.selectedProjects, id] + })); + } }); }; @@ -81,9 +91,11 @@ export default class Projects extends React.PureComponent { projectId: id }).then( () => { - this.setState(state => ({ - selectedProjects: without(state.selectedProjects, id) - })); + if (this.mounted) { + this.setState(state => ({ + selectedProjects: without(state.selectedProjects, id) + })); + } }, () => {} ); -- 2.39.5