Skip to content

fix(NativeEngineHook): avoid deprecated warning #472

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

Merged

Conversation

RodSarhan
Copy link
Contributor

@RodSarhan RodSarhan commented Oct 7, 2022

Fixed useAppState hook causing "removeEventListener" deprecated warning on newer react native versions

Describe the change

  • Checks if the return of AppState.addEventListener has a remove method
  • If the remove method exists it will be called
  • if the remove method doesn't exist, the event listener will be removed with the deprecated removeEventListener method

Documentation

N/A

Testing

Tested on Android and iOS

Fix useAppState hook causing "removeEventListener" deprecated warning on newer react native versions
Tried to keep it backward compatible by checking if the remove method exists
@ryantrem
Copy link
Member

ryantrem commented Oct 7, 2022

Thanks for the contribution @RodSarhan! It looks like this branch is out-of-date with master. Can you merge master, either locally or I think you can directly in the PR as well?

@RodSarhan
Copy link
Contributor Author

@ryantrem It should be good now, thanks for the awesome work you guys are doing!

@ryantrem ryantrem enabled auto-merge (squash) October 7, 2022 17:07
@ryantrem ryantrem merged commit 1973bf2 into BabylonJS:master Oct 7, 2022
@RodSarhan
Copy link
Contributor Author

RodSarhan commented Oct 7, 2022

I noticed that TS complained about the method "remove" not existing on appStateListener on react native 64
We can avoid that by asserting the type or ignoring the error, please let me know what you prefer

const appStateListener = AppState.addEventListener("change", onAppStateChanged);
// Asserting the type to prevent TS type errors on older RN versions
const removeListener = appStateListener?.["remove"] as undefined | Function;

return () => {
    if (removeListener) {
        removeListener();
    }
    else {
        AppState.removeEventListener("change", onAppStateChanged);
    }
};

@CedricGuillemet
Copy link
Contributor

@ryantrem any opinion on this one?

@ryantrem
Copy link
Member

Oy did this PR merge even though there was a TS build error? I wonder if we have our checks misconfigured...
I think the suggested change above seems reasonable. If it fixes a TS build break, then it'd be great if we could get the change soon, and then we can always consider alternatives in the future.

@RodSarhan
Copy link
Contributor Author

#475 should fix this

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.

3 participants