Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes error when navigationBar is set back to null #4941

Closed
wants to merge 4 commits into from

Conversation

gre
Copy link
Contributor

@gre gre commented Dec 23, 2015

This fixes a regression introduced in df70005

If you set navigationBar props (on Navigator) and then later set it back to null, it will crashes.
(N.B. this should be possible as navigationBar is optional)

cc @satya164

gre referenced this pull request Dec 23, 2015
Summary: Before that it was not possible to get a ref to a navigation bar (unless using Navigator's internal `_navBar` prop)
Closes #3755

Reviewed By: svcscm

Differential Revision: D2674315

Pulled By: nicklockwood

fb-gh-sync-id: 26120f7bcbb675e8217b8bd963dcc6ed314d4ba3
@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @despairblue, @ericvicenti and @hedgerwang to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Dec 23, 2015
@@ -1088,7 +1088,10 @@ var Navigator = React.createClass({
}
return React.cloneElement(this.props.navigationBar, {
ref: (navBar) => {
this.props.navigationBar.ref instanceof Function && this.props.navigationBar.ref(navBar);
var navigationBar = this.props.navigationBar;
if (navigationBar && navigationBar.ref instanceof Function) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why navigationBar.ref instanceof Function was used instead of typeof navigationBar.ref === 'function'

@satya164
Copy link
Contributor

cc @mkonicek

@facebook-github-bot
Copy link
Contributor

@gre updated the pull request.

@@ -1088,7 +1088,10 @@ var Navigator = React.createClass({
}
return React.cloneElement(this.props.navigationBar, {
ref: (navBar) => {
this.props.navigationBar.ref instanceof Function && this.props.navigationBar.ref(navBar);
var navigationBar = this.props.navigationBar;
if (navigationBar && typeof navigationBar.ref === "function") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single quoted strings

@facebook-github-bot
Copy link
Contributor

@gre updated the pull request.

@ide
Copy link
Contributor

ide commented Dec 23, 2015

This fix might actually be unsafe since it's dynamically looking up this.props.navigationBar which changes over time. Here's what I think the right fix is:

  _renderNavigationBar: function() {
    let { navigatorBar } = this.props;  // <---- keeping this element reference is the important part
    if (!navigationBar) {
      return null;
    }
    return React.cloneElement(navigationBar, {
      ref: (navBar) => {
        this._navBar = navBar;
        if (navigationBar && typeof navigationBar.ref === 'function') {
          navigationBar.ref(navBar);
        }
      },
      navigator: this._navigationBarNavigator,
      navState: this.state,
    });
  },

Awhile back I wrote an npm package to help with this:

let cloneReferencedElement = require('react-native-clone-referenced-element');

  _renderNavigationBar: function() {
    if (!this.props.navigationBar) {
      return null;
    }
    return cloneReferencedElement(this.props.navigationBar, {
      ref: navBar => { this._navBar = navBar; },
      navigator: this._navigationBarNavigator,
      navState: this.state,
    });
  },

Either of these solutions would work I think.

@facebook-github-bot
Copy link
Contributor

@gre updated the pull request.

@gre
Copy link
Contributor Author

gre commented Dec 24, 2015

oh yeah, I was wondering this when I first read the code, keeping the same navigationBar make sense, then maybe we don't even need to internal check if (navigationBar) because the falsy case is skipped?

@ide
Copy link
Contributor

ide commented Dec 24, 2015

Calling cloneElement on null will throw an error I think, so this looks correct.

@ide
Copy link
Contributor

ide commented Dec 24, 2015

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/436748359869262/int_phab to review.

@gre
Copy link
Contributor Author

gre commented Dec 24, 2015

Thanks @ide

@gre
Copy link
Contributor Author

gre commented Dec 29, 2015

why is it failed?

@satya164
Copy link
Contributor

@gre There was some issue with the shipit bot. Lemme try again.

@satya164
Copy link
Contributor

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/436748359869262/int_phab to review.

@gre
Copy link
Contributor Author

gre commented Jan 1, 2016

is it related to the Travis build?

@satya164
Copy link
Contributor

satya164 commented Jan 2, 2016

I've no idea. Let's wait for @mkonicek to return from his vacation. He'll know why it's happening :)

@dsibiski
Copy link
Contributor

dsibiski commented Jan 2, 2016

Thanks a ton @gre, this saved the day! 👍

@gre
Copy link
Contributor Author

gre commented Jan 5, 2016

should I open a new PR ?

@satya164
Copy link
Contributor

satya164 commented Jan 5, 2016

@gre You shouldn't need to. Ping @mkonicek

@mkonicek
Copy link
Contributor

mkonicek commented Jan 6, 2016

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

@gre updated the pull request.

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/436748359869262/int_phab to review.

@ghost ghost closed this in 4f9086f Jan 7, 2016
@gre
Copy link
Contributor Author

gre commented Jan 7, 2016

do you know if this land in 0.18 or later? thanks

@satya164
Copy link
Contributor

satya164 commented Jan 7, 2016

We could cherry-pick this to 0.18.0.

csudanthi pushed a commit to healthiqeng/react-native that referenced this pull request Jan 16, 2016
Summary:
This fixes a regression introduced in facebook@df70005

If you set navigationBar props (on Navigator) and then later set it back to null, it will crashes.
(N.B. this should be possible as navigationBar is optional)

cc satya164
Closes facebook#4941

Reviewed By: svcscm

Differential Revision: D2788889

Pulled By: bestander

fb-gh-sync-id: f8f1cd6cc2ce13b1b1b86fa76d3b22c26a8adb5b
mkonicek pushed a commit that referenced this pull request Jan 18, 2016
Summary:
This fixes a regression introduced in df70005

If you set navigationBar props (on Navigator) and then later set it back to null, it will crashes.
(N.B. this should be possible as navigationBar is optional)

cc satya164
Closes #4941

Reviewed By: svcscm

Differential Revision: D2788889

Pulled By: bestander

fb-gh-sync-id: f8f1cd6cc2ce13b1b1b86fa76d3b22c26a8adb5b
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. JavaScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants