Skip to content

Commit

Permalink
Remove an event listener in componentWillUnmount
Browse files Browse the repository at this point in the history
Event listener for event securitypolicyviolation [:36] added in componentDidMount [:35] is never removed from document [:36] , remove it in lifecycle method 'componentWillUnmount'.
  • Loading branch information
hodovani authored Sep 25, 2020
1 parent ec06fa2 commit 7564598
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions csp-server/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ export default class App extends Component {
}

componentDidMount() {
document.addEventListener('securitypolicyviolation', (e) => {
this.setState((state) => {
return { cspErrors: [...state.cspErrors, e] };
});
});
document.addEventListener(
'securitypolicyviolation',
this.onSecurityPolicyViolation,
);
}

componentWillUnmount() {
document.removeEventListener(
'securitypolicyviolation',
this.onSecurityPolicyViolation,
);
}

onDragEnd(result) {
Expand All @@ -57,6 +63,12 @@ export default class App extends Component {
});
}

onSecurityPolicyViolation = (e) => {
this.setState((state) => {
return { cspErrors: [...state.cspErrors, e] };
});
};

// Normally you would want to split things out into separate components.
// But in this example everything is just done in one place for simplicity
render() {
Expand Down

0 comments on commit 7564598

Please sign in to comment.