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

Improve root component ref definition #2705

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 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
// 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 <RootComponent {...props} />
}
}

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

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