Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 7564598

Browse files
authored
Remove an event listener in componentWillUnmount
Event listener for event securitypolicyviolation [:36] added in componentDidMount [:35] is never removed from document [:36] , remove it in lifecycle method 'componentWillUnmount'.
1 parent ec06fa2 commit 7564598

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

csp-server/app.jsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ export default class App extends Component {
3333
}
3434

3535
componentDidMount() {
36-
document.addEventListener('securitypolicyviolation', (e) => {
37-
this.setState((state) => {
38-
return { cspErrors: [...state.cspErrors, e] };
39-
});
40-
});
36+
document.addEventListener(
37+
'securitypolicyviolation',
38+
this.onSecurityPolicyViolation,
39+
);
40+
}
41+
42+
componentWillUnmount() {
43+
document.removeEventListener(
44+
'securitypolicyviolation',
45+
this.onSecurityPolicyViolation,
46+
);
4147
}
4248

4349
onDragEnd(result) {
@@ -57,6 +63,12 @@ export default class App extends Component {
5763
});
5864
}
5965

66+
onSecurityPolicyViolation = (e) => {
67+
this.setState((state) => {
68+
return { cspErrors: [...state.cspErrors, e] };
69+
});
70+
};
71+
6072
// Normally you would want to split things out into separate components.
6173
// But in this example everything is just done in one place for simplicity
6274
render() {

0 commit comments

Comments
 (0)