Skip to content

Commit

Permalink
Improve root component ref definition (#2705)
Browse files Browse the repository at this point in the history
* improve root component ref definition

* check RootComponent.prototype for null
  • Loading branch information
MikhailSuendukov authored Jul 3, 2024
1 parent c4b293d commit 6c5bdd9
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions CodePush.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
}
Expand All @@ -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
if (RootComponent.prototype.render) {
props.ref = "rootComponent";
// We can set ref property on class components only (not stateless)
// Check it by render method
if (RootComponent.prototype && RootComponent.prototype.render) {
props.ref = this.rootComponentRef;
}

return <RootComponent {...props} />
}
}

return hoistStatics(extended, RootComponent);
return hoistStatics(CodePushComponent, RootComponent);
}

if (typeof options === "function") {
Expand Down

0 comments on commit 6c5bdd9

Please sign in to comment.