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

Warning - 'getNode() deprecation fix' #673

Closed
wants to merge 2 commits into from

Conversation

dilinade
Copy link

@dilinade dilinade commented Mar 29, 2020

Platforms affected

Android & iOS

What does this PR do?

Fixes #672

What testing has been done on this change?

Tested features checklist

@yasaricli
Copy link

+1

@bd-arc
Copy link
Contributor

bd-arc commented Apr 3, 2020

@dilinade Thanks for the PR!

I'd be happy to merge this, but I fear this might break things for users who haven't migrated to the latest version of React Native...

Think you can make it backward-compatible?

@dilinade
Copy link
Author

dilinade commented Apr 4, 2020

@bd-arc Hi, I have made some changes. Please review :)
Thank you.

@ifsnow
Copy link
Contributor

ifsnow commented Apr 6, 2020

@dilinade I think it's not efficient to check the version of React Native every time in _getWrappedRef(). Why not reuse it after checking it only once?

@bd-arc
Copy link
Contributor

bd-arc commented Apr 6, 2020

@dilinade Couple of notes:

  1. I agree with @ifsnow: this should be done only once in the constructor.
  2. It would be better to have a future-proof way of determining the version.
  3. We probably want to err of the side of caution and make sure that everything is defined.
  4. Please make sure to activate Eslint when working on the project ;-)

Putting it all together, here's my suggestion (untested). What do you guys think?

constructor (props) {
    super(props);

    // Everything else...

    this._shouldUseGetNode = this._useGetNode();
}

_useGetNode () {
    const package = require('../../../react-native/package.json');
    const version = package && package.version;
    if (!version) {
        return true;
    }
    const versionSplit = version.split('.');
    if (!versionSplit || !versionSplit.length) {
        return true;
    }
    const versionCode = versionSplit[0] * 1000000 + versionSplit[1] * 10000 + versionSplit[2] * 100;
    return versionCode < 620000; // RN 0.62
}

_getWrappedRef () {
    // https://github.com/facebook/react-native/issues/10635
    // https://stackoverflow.com/a/48786374/8412141
    return this._shouldUseGetNode ?
        this._carouselRef && this._carouselRef.getNode && this._carouselRef.getNode() :
        this._carouselRef;
}

@bd-arc
Copy link
Contributor

bd-arc commented Apr 6, 2020

@dilinade This is how I actually implemented it in version 4.x of the plugin I'm currently working on: 1b78117

@dilinade
Copy link
Author

dilinade commented Apr 6, 2020

@bd-arc Got it and Cool :)

@timgarciaa
Copy link

i'm using this version 'react-native-snap-carousel@4.0.0-beta.2' but still got the error. Is this the same with this release?

@dotamir
Copy link

dotamir commented Apr 17, 2020

@dilinade Did you made the suggested changes? Is this PR ready to go forward?

@nickarora
Copy link

nickarora commented Apr 20, 2020

@dilinade @bd-arc -- can we get this merged and released?

@bd-arc
Copy link
Contributor

bd-arc commented Apr 20, 2020

Unfortunately the following basically breaks everything on Snack because it is undefined there: require('../../../react-native/package.json'). I assume Expo uses a different path and that it will break for all Expo users as well.

This is a quite a big issue and I'm scratching my head to find a way to make it work.

Requiring the package conditionally would be the obvious way to go, but unfortunately this is not supported by the React Native packager (see this thread for example).

Also tried with a try/catch, but encountered the same issue (conditional require not supported by metro bundle).

If anyone has an idea to address that, please let everyone know!

@bd-arc bd-arc mentioned this pull request Apr 20, 2020
@r0b0t3d
Copy link
Contributor

r0b0t3d commented Apr 20, 2020

Could we check if _carouselRef contains setNativeProps function, so we do not need to check the version anymore.

if (this._carouselRef && this._carouselRef.setNativeProps) {
  return this._carouselRef;
}
return this._carouselRef && this._carouselRef.getNode && this._carouselRef.getNode();

@dotamir
Copy link

dotamir commented Apr 20, 2020

@bd-arc I guess Expo is using this to load the files: expo-asset

I'm not sure if we use this, that's gonna support RN too or not. Which I think it will because expo is supporting most of React Native API's these days.

Also take a look at this too: here

@vamshi9666
Copy link

+1

@bfaulk96
Copy link

Could we check if _carouselRef contains setNativeProps function, so we do not need to check the version anymore.

if (this._carouselRef && this._carouselRef.setNativeProps) {
  return this._carouselRef;
}
return this._carouselRef && this._carouselRef.getNode && this._carouselRef.getNode();

Well, this definitely seems to solve the problem on 0.62.2, can someone confirm that it still works on <0.60.0? Would be great to get a fix as simple as this into the beta

@bd-arc
Copy link
Contributor

bd-arc commented May 26, 2020

Just included @r0b0t3d's suggestion in the v4 beta branch and published version 4.0.0-beta.3 if you want to give it a try.

Note that I didn't test it on RN pre-0.6x.

If someone wants to jump in...

@bd-arc
Copy link
Contributor

bd-arc commented May 26, 2020

Unfortunately, the suggestion doesn't work on versions < 0.62.

Back to square one...

@bd-arc
Copy link
Contributor

bd-arc commented May 27, 2020

Fixed in version 3.9.1 with backward-compatible code.

@bd-arc bd-arc closed this May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning : Calling 'getNode()' on the ref an Animated Component is no longer necessary.
10 participants