From 6cf819cef4158d77ea93023e0092969e44ada774 Mon Sep 17 00:00:00 2001 From: hamed-zeidabadi Date: Fri, 7 Nov 2025 20:06:59 +0330 Subject: [PATCH] fix: replace deprecated componentWillMount and unsafe hasOwnProperty usage Replace deprecated componentWillMount lifecycle method with componentDidMount to prevent memory leaks and follow React best practices. Also replace direct hasOwnProperty calls with Object.prototype.hasOwnProperty.call() to avoid issues with objects that don't inherit from Object.prototype or have overridden hasOwnProperty method. Changes: - Move history.listen setup from componentWillMount to componentDidMount - Replace data.hasOwnProperty() with Object.prototype.hasOwnProperty.call() - Replace params.hasOwnProperty() with Object.prototype.hasOwnProperty.call() This fixes potential memory leaks and follows ESLint no-prototype-builtins rule. --- src/pages/XxxAnswersPage/XxxAnswersPage.tsx | 11 +++++------ src/pages/XxxQuestionsPage/XxxQuestionsPage.tsx | 7 +++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/pages/XxxAnswersPage/XxxAnswersPage.tsx b/src/pages/XxxAnswersPage/XxxAnswersPage.tsx index 4524ce8..ecf3dff 100644 --- a/src/pages/XxxAnswersPage/XxxAnswersPage.tsx +++ b/src/pages/XxxAnswersPage/XxxAnswersPage.tsx @@ -44,9 +44,8 @@ class XxxAnswersPage extends React.Component< componentDidMount() { this.getQuestionId(); this.getQuestion(); - } - - componentWillMount() { + + // Set up history listener after component is mounted this.unlisten = this.props.history.listen(() => { console.log("url changed"); this.getQuestionId(); @@ -62,7 +61,7 @@ class XxxAnswersPage extends React.Component< let id: string = ""; let params: XxxAnswersPageSearchParams; params = this.props.match.params; - if (params.hasOwnProperty("id") && typeof params.id === "string") { + if (Object.prototype.hasOwnProperty.call(params, "id") && typeof params.id === "string") { this.questionId = params.id; } return id; @@ -98,7 +97,7 @@ class XxxAnswersPage extends React.Component< response.json().then(data => { if ( typeof data === "object" && - data.hasOwnProperty("items") && + Object.prototype.hasOwnProperty.call(data, "items") && Array.isArray(data.items) && data.items.length > 0 ) { @@ -138,7 +137,7 @@ class XxxAnswersPage extends React.Component< response.json().then(data => { if ( typeof data === "object" && - data.hasOwnProperty("items") && + Object.prototype.hasOwnProperty.call(data, "items") && Array.isArray(data.items) && data.items.length > 0 ) { diff --git a/src/pages/XxxQuestionsPage/XxxQuestionsPage.tsx b/src/pages/XxxQuestionsPage/XxxQuestionsPage.tsx index eeea9f0..c631e67 100644 --- a/src/pages/XxxQuestionsPage/XxxQuestionsPage.tsx +++ b/src/pages/XxxQuestionsPage/XxxQuestionsPage.tsx @@ -50,9 +50,8 @@ class XxxQuestionsPage extends React.Component< componentDidMount() { this.readUrlQueryString(this.props.location.search); this.getQuestions(); - } - - componentWillMount() { + + // Set up history listener after component is mounted this.unlisten = this.props.history.listen(location => { this.readUrlQueryString(location.search); this.getQuestions(); @@ -104,7 +103,7 @@ class XxxQuestionsPage extends React.Component< response.json().then(data => { if ( typeof data === "object" && - data.hasOwnProperty("items") && + Object.prototype.hasOwnProperty.call(data, "items") && Array.isArray(data.items) && data.items.length > 0 ) {