Skip to content

Commit 6c5bdd9

Browse files
Improve root component ref definition (#2705)
* improve root component ref definition * check RootComponent.prototype for null
1 parent c4b293d commit 6c5bdd9

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

CodePush.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -531,42 +531,41 @@ function codePushify(options = {}) {
531531
);
532532
}
533533

534-
var decorator = (RootComponent) => {
535-
const extended = class CodePushComponent extends React.Component {
534+
const decorator = (RootComponent) => {
535+
class CodePushComponent extends React.Component {
536+
constructor(props) {
537+
super(props);
538+
this.rootComponentRef = React.createRef();
539+
}
540+
536541
componentDidMount() {
537542
if (options.checkFrequency === CodePush.CheckFrequency.MANUAL) {
538543
CodePush.notifyAppReady();
539544
} else {
540-
let rootComponentInstance = this.refs.rootComponent;
545+
const rootComponentInstance = this.rootComponentRef.current;
541546

542547
let syncStatusCallback;
543548
if (rootComponentInstance && rootComponentInstance.codePushStatusDidChange) {
544-
syncStatusCallback = rootComponentInstance.codePushStatusDidChange;
545-
if (rootComponentInstance instanceof React.Component) {
546-
syncStatusCallback = syncStatusCallback.bind(rootComponentInstance);
547-
}
549+
syncStatusCallback = rootComponentInstance.codePushStatusDidChange.bind(rootComponentInstance);
548550
}
549551

550552
let downloadProgressCallback;
551553
if (rootComponentInstance && rootComponentInstance.codePushDownloadDidProgress) {
552-
downloadProgressCallback = rootComponentInstance.codePushDownloadDidProgress;
553-
if (rootComponentInstance instanceof React.Component) {
554-
downloadProgressCallback = downloadProgressCallback.bind(rootComponentInstance);
555-
}
554+
downloadProgressCallback = rootComponentInstance.codePushDownloadDidProgress.bind(rootComponentInstance);
556555
}
557556

558557
let handleBinaryVersionMismatchCallback;
559558
if (rootComponentInstance && rootComponentInstance.codePushOnBinaryVersionMismatch) {
560-
handleBinaryVersionMismatchCallback = rootComponentInstance.codePushOnBinaryVersionMismatch;
561-
if (rootComponentInstance instanceof React.Component) {
562-
handleBinaryVersionMismatchCallback = handleBinaryVersionMismatchCallback.bind(rootComponentInstance);
563-
}
559+
handleBinaryVersionMismatchCallback = rootComponentInstance.codePushOnBinaryVersionMismatch.bind(rootComponentInstance);
564560
}
565561

566562
CodePush.sync(options, syncStatusCallback, downloadProgressCallback, handleBinaryVersionMismatchCallback);
563+
567564
if (options.checkFrequency === CodePush.CheckFrequency.ON_APP_RESUME) {
568565
ReactNative.AppState.addEventListener("change", (newState) => {
569-
newState === "active" && CodePush.sync(options, syncStatusCallback, downloadProgressCallback);
566+
if (newState === "active") {
567+
CodePush.sync(options, syncStatusCallback, downloadProgressCallback);
568+
}
570569
});
571570
}
572571
}
@@ -575,17 +574,17 @@ function codePushify(options = {}) {
575574
render() {
576575
const props = {...this.props};
577576

578-
// we can set ref property on class components only (not stateless)
579-
// check it by render method
580-
if (RootComponent.prototype.render) {
581-
props.ref = "rootComponent";
577+
// We can set ref property on class components only (not stateless)
578+
// Check it by render method
579+
if (RootComponent.prototype && RootComponent.prototype.render) {
580+
props.ref = this.rootComponentRef;
582581
}
583582

584583
return <RootComponent {...props} />
585584
}
586585
}
587586

588-
return hoistStatics(extended, RootComponent);
587+
return hoistStatics(CodePushComponent, RootComponent);
589588
}
590589

591590
if (typeof options === "function") {

0 commit comments

Comments
 (0)