From 5de1a62220f56dfd0518fbf28d03b7767d6e7952 Mon Sep 17 00:00:00 2001 From: JiglioNero Date: Fri, 7 Jun 2024 17:32:22 +0200 Subject: [PATCH 1/2] improve root component ref definition --- CodePush.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/CodePush.js b/CodePush.js index b9a9a19ce..3c1338f28 100644 --- a/CodePush.js +++ b/CodePush.js @@ -531,42 +531,41 @@ function codePushify(options = {}) { ); } - var decorator = (RootComponent) => { - const extended = class CodePushComponent extends React.Component { + const decorator = (RootComponent) => { + class CodePushComponent extends React.Component { + constructor(props) { + super(props); + this.rootComponentRef = React.createRef(); + } + componentDidMount() { if (options.checkFrequency === CodePush.CheckFrequency.MANUAL) { CodePush.notifyAppReady(); } else { - let rootComponentInstance = this.refs.rootComponent; + const rootComponentInstance = this.rootComponentRef.current; let syncStatusCallback; if (rootComponentInstance && rootComponentInstance.codePushStatusDidChange) { - syncStatusCallback = rootComponentInstance.codePushStatusDidChange; - if (rootComponentInstance instanceof React.Component) { - syncStatusCallback = syncStatusCallback.bind(rootComponentInstance); - } + syncStatusCallback = rootComponentInstance.codePushStatusDidChange.bind(rootComponentInstance); } let downloadProgressCallback; if (rootComponentInstance && rootComponentInstance.codePushDownloadDidProgress) { - downloadProgressCallback = rootComponentInstance.codePushDownloadDidProgress; - if (rootComponentInstance instanceof React.Component) { - downloadProgressCallback = downloadProgressCallback.bind(rootComponentInstance); - } + downloadProgressCallback = rootComponentInstance.codePushDownloadDidProgress.bind(rootComponentInstance); } let handleBinaryVersionMismatchCallback; if (rootComponentInstance && rootComponentInstance.codePushOnBinaryVersionMismatch) { - handleBinaryVersionMismatchCallback = rootComponentInstance.codePushOnBinaryVersionMismatch; - if (rootComponentInstance instanceof React.Component) { - handleBinaryVersionMismatchCallback = handleBinaryVersionMismatchCallback.bind(rootComponentInstance); - } + handleBinaryVersionMismatchCallback = rootComponentInstance.codePushOnBinaryVersionMismatch.bind(rootComponentInstance); } CodePush.sync(options, syncStatusCallback, downloadProgressCallback, handleBinaryVersionMismatchCallback); + if (options.checkFrequency === CodePush.CheckFrequency.ON_APP_RESUME) { ReactNative.AppState.addEventListener("change", (newState) => { - newState === "active" && CodePush.sync(options, syncStatusCallback, downloadProgressCallback); + if (newState === "active") { + CodePush.sync(options, syncStatusCallback, downloadProgressCallback); + } }); } } @@ -575,17 +574,17 @@ function codePushify(options = {}) { render() { const props = {...this.props}; - // we can set ref property on class components only (not stateless) - // check it by render method + // We can set ref property on class components only (not stateless) + // Check it by render method if (RootComponent.prototype.render) { - props.ref = "rootComponent"; + props.ref = this.rootComponentRef; } return } } - return hoistStatics(extended, RootComponent); + return hoistStatics(CodePushComponent, RootComponent); } if (typeof options === "function") { From 0d4baf7978b8e455c803711f31ba0c14f73f7fdb Mon Sep 17 00:00:00 2001 From: JiglioNero Date: Tue, 2 Jul 2024 16:04:10 +0200 Subject: [PATCH 2/2] check RootComponent.prototype for null --- CodePush.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodePush.js b/CodePush.js index 3c1338f28..2d1d9deda 100644 --- a/CodePush.js +++ b/CodePush.js @@ -576,7 +576,7 @@ function codePushify(options = {}) { // We can set ref property on class components only (not stateless) // Check it by render method - if (RootComponent.prototype.render) { + if (RootComponent.prototype && RootComponent.prototype.render) { props.ref = this.rootComponentRef; }